SoFunction
Updated on 2025-04-09

Android downloads network pictures and displays them locally

The process of downloading network pictures on Android is:

Send a network request -> Download the image in the form of a stream -> convert the stream into a Bitmap and assign it to the ImageView control.

Note

  • The latest Android system cannot request the network on the main thread, and it needs to use threads to request it.
  • Downloading images is a time-consuming task. The best way is to operate it in an AsyncTask.

Design ideas

1. Network request: The file type that needs to be downloaded in this example is the image type. The data type obtained by the network request can be converted to Bitmap that has been used directly for ImageView. However, a reasonable network request class design is to convert the downloaded data type into the most basic InputStream, so that no matter it is downloading images, audio, text or video, the results can be thrown out for the upper-level logic to process.

2. Asynchronous request: Downloading pictures. Network downloading is a time-consuming operation, so it is necessary to encapsulate an AsyncTask to process network requests, which is inherited from the Runnable interface.

3. Asynchronous callback: The foreground needs to obtain the image source through callbacks and assign the image source to ImageView.

Related Codes

NetService: Network request service class

public class NetService {
public static InputStream getInputStreamByUrl(String address){
URL url = null;
HttpURLConnection urlConnection = null;
try {
url = new URL(address);
urlConnection = (HttpURLConnection) ();
(2 * 1000);
("GET");
return ();
} catch (IOException e) {
();
}
return null;
}
}

NetServiceTask: AsyncTask class

import ;
import ;
import ;
import ;
import ;
public class NetServiceTask extends AsyncTask
public NetServiceTask(String address, URLPostHandler urlPostHandler) {
 =address;
 =urlPostHandler;
}
/**
  * Indicates that the task has performed the previous operation
  */
@Override
protected void onPreExecute() {
 // TODO Auto-generated method stub
 ();
}
/**
  * Mainly to complete time-consuming operations
  */
@Override
protected Bitmap doInBackground(String... arg0) {
 InputStream inputStream=(arg0[0]);
 if(inputStream!=null){
  return (new BufferedInputStream(inputStream));
 }
 return null;
}
/**
  * Mainly update the UI operation
  */
@Override
protected void onPostExecute(Bitmap result) {
 // TODO Auto-generated method stub
 (result);
 if(!=null&&result!=null){
  (result);
 }
}
@Override
public void run() {
 execute();
}
}

URLPostHandler: callback interface

public interface URLPostHandler {
void PostHandler(Bitmap bitmap);
}

The front desk requests the image and displays it to ImageView

public class MainActivity extends AppCompatActivity {
Button buttonDownload;
ImageView imageViewImg;
@Override
protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.activity_main);
 buttonDownload = (Button) findViewById();
 imageViewImg = (ImageView) findViewById();
 (new () {
 @Override
 public void onClick(View v) {
  String address = "/timg?image&quality=80&size=b9999_10000&sec=1490783056273&di=6160d101d31dcf5f44b443ad9c5b2648&imgtype=0&src=http%3A%2F%2Fimg.%2Fuploads%2Fallimg%2F110626%";
  NetServiceTask netServerTask= new NetServiceTask(address,new URLPostHandler() {
  @Override
  public void PostHandler(Bitmap bitmap) {
   (bitmap);
  }
  });
  Thread thread=new Thread(netServerTask);
  ();
 }
 });
}
}

The above is all the content of this article. I hope that the content of this article will help you study or work. I also hope to support me more!