Android developed a method for loading images

  • 2020-06-19 11:43:25
  • OfStack

An example of Android's image loading method is presented. Share to everybody for everybody reference. The specific analysis is as follows:

To load images on the network, you need to configure network access permissions in manifest, as follows:

< uses-permission android:name="android.permission.INTERNET" / >

If this permission is not configured, an error is reported: unknown host exception.


package com.example.loadimgfromweb; 
import java.io.InputStream; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.ImageView; 
public class MainActivity extends Activity { 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ImageView v = (ImageView) findViewById(R.id.imageView1); 
    String url = "http://minimg.hexun.com/i1.hexunimg.cn/2011-11-07/134970028_200x150.jpg"; 
    new DownLoadImage(v).execute(url); 
  } 
  @Override 
  public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
  } 
  public class DownLoadImage extends AsyncTask<String, Void, Bitmap> {
    ImageView imageView; 
    public DownLoadImage(ImageView imageView) { 
      this.imageView = imageView; 
    } 
    @Override 
    protected Bitmap doInBackground(String... urls) { 
      String url = urls[0]; 
      Bitmap tmpBitmap = null; 
      try { 
        InputStream is = new java.net.URL(url).openStream(); 
        tmpBitmap = BitmapFactory.decodeStream(is); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
      return tmpBitmap; 
    } 
    @Override 
    protected void onPostExecute(Bitmap result) { 
      imageView.setImageBitmap(result); 
    } 
  } 
}

In the above example, AsyncTask is used to interact with the front-end user interface. Avoid long loading time, which will affect the interactivity of the interface.

If you want to load a local image, go straight to:


ImageView v = (ImageView) findViewById(R.id.imageView1); 
    v.setImageResource(R.drawable.apic); 

When loading images using AsyncTask, you can also save them locally:


@Override 
protected Bitmap doInBackground(String... urls) { 
  String url = urls[0]; 
  Bitmap tmpBitmap = null; 
  try { 
 InputStream is = new java.net.URL(url).openStream(); 
 URL u = new URL(url); 
 HttpURLConnection conn = (HttpURLConnection) u.openConnection();
 Bitmap bitmap = BitmapFactory.decodeStream(is); 
 //  Determine if there is a memory card  
 if (Environment.getExternalStorageState().equals( 
 Environment.MEDIA_MOUNTED)) {
   //  Save the image locally  
   File sdCardDir = Environment.getExternalStorageDirectory();
   String fileName = "a.jpg"; 
   String filePath = sdCardDir + "/" + fileName; 
   FileOutputStream fos = new FileOutputStream(filePath); 
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
 } 
 //  Close the stream  
 is.close(); 
 conn.disconnect(); 
 return bitmap; 
  } catch (Exception e) { 
 e.printStackTrace(); 
  } 
  return tmpBitmap; 
} 

Hopefully, this article has helped you with your Android programming.


Related articles: