Android development-network image acquisition
Used for completing the settings! Obtain network images
Here is a brief introduction to my situation.
First, design the layout. Drag a few widgets and then click OK.
Start adding features in Acitivity
Take a picture of the link online: http://www.chzu.edu.cn/page/main1152/images/banner.jpg
Paste the main code:
private Bitmap getUrlImageMap() {URL imageUrl = null;Bitmap bitmap = null;try {imageUrl = new URL(url);HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();conn.connect();InputStream is = conn.getInputStream();bitmap = BitmapFactory.decodeStream(is);is.close();} catch (Exception e) {e.printStackTrace();}return bitmap;}
The above is to get the bitmap value of the image through the HttpUrlConnection link, and then write a class that inherits the AsyncTask to load the image.
Pay attention to doInbackground () and onPostExecute (). The former processes time-consuming operations and the latter processes UI updates. Do not make a mistake in this regard.
class loadImage extends AsyncTask
{Bitmap aBitmap;@Overrideprotected Void doInBackground(Void... params) {aBitmap = getUrlImageMap();return null;}@Overrideprotected void onPostExecute(Void result) {super.onPostExecute(result);imageView.setImageBitmap(aBitmap);}}
Run, the interface is as follows
The example is very simple. Pay attention to the acquisition of one piece and asynchronous loading and UI updates. Click here to download the DEMO.