SoFunction
Updated on 2025-04-10

Android development method to load network pictures and download them to local SdCard

This article describes the method of Android development to load network images and download them to local SdCard. Share it for your reference, as follows:

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class MainActivity extends Activity {
  private ImageView mImageView;
  private String imgPath="/2007-11-09/200711912453162_2.jpg";
  private File cache;//Cache path  Handler handler=new Handler(new () {
    @Override
    public boolean handleMessage(Message msg) {
      //show      Bitmap b=(Bitmap);
      (b);
      //Save to local      File imgFile=new File(cache,"");
      try {
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(imgFile));
        (,80,bos);
        ();
        ();
      } catch (Exception e) {
        // TODO Auto-generated catch block
        ();
      }
      return false;
    }
  });
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView(.activity_main);
    mImageView=(ImageView)findViewById();
    //Create cache path    //() Get the root path of the mobile phone memory card    cache=new File((),"Test");
    if(!()){
      ();
    }
    //Time-consuming operations must be placed in the child thread operation    //Open the child thread to get the input stream    new Thread(new Runnable() {
      @Override
      public void run() {
        HttpURLConnection conn=null;
        InputStream is=null;
        try {
          URL url=new URL(imgPath);
          //Open the connection          conn=(HttpURLConnection) ();
          //Set connection timeout          (5000);
          //Set request method          ("GET");
          //();
          if(()==200){
            is=();
            Bitmap b=(is);
            //Convert the input stream into bitmap format and send it to the main thread in msg form            Message msg=new Message();
            =b;
            (msg);
          }
        } catch (Exception e) {
          ();
        }finally{
          try {
          //Remember to close after use            ();
            ();
          } catch (IOException e) {
            ();
          }
        }
      }
    }).start();
  }
}

Remember to add these three permissions to the file

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

Also, you can click here to viewAndroid permissions operation instructions

For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.