Android downloads network pictures and displays them locally

The process of downloading network pictures for Android is as follows:

Send a network request - > download the picture as a stream - > convert the stream into a bitmap and assign it to the ImageView control.

Attention

Design ideas

1. Network request: in this example, the file type to be downloaded is the image type. The data type obtained by the network request can be converted into bitmap, which has been used directly by ImageView. However, a reasonable design of the network request class is to convert the downloaded data type into the most basic InputStream, so that whether downloading pictures, audio, text or video, The result can be thrown out for the upper 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 the network request, which is inherited from the runnable interface.

3. Asynchronous callback: the foreground needs to get the picture source through callback and assign the picture source to ImageView

Related code

Netservice: network request service class

Netservicetask: asynctask class

Urlposthandler: callback interface

The foreground requests the picture and displays it to ImageView

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>