Android网络下载图片

 1 package net.learn2develop.Networking;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 
 9 import java.net.HttpURLConnection;
10 import java.net.URL;
11 import java.net.URLConnection;
12 import android.util.Log;
13 
14 import android.widget.ImageView;
15 import android.widget.Toast;
16 import android.graphics.Bitmap;
17 import android.graphics.BitmapFactory;
18 import android.os.AsyncTask;
19 public class NetworkingActivity extends Activity {
20         
21     ImageView img;    
22     
23     private InputStream OpenHttpConnection(String urlString) throws IOException
24     {
25         InputStream in = null;
26         int response = -1;
27                
28         URL url = new URL(urlString); 
29         URLConnection conn = url.openConnection();
30                  
31         if (!(conn instanceof HttpURLConnection))                     
32             throw new IOException("Not an HTTP connection");        
33         try{
34             HttpURLConnection httpConn = (HttpURLConnection) conn;
35             httpConn.setAllowUserInteraction(false);
36             httpConn.setInstanceFollowRedirects(true);
37             httpConn.setRequestMethod("GET");
38             httpConn.connect();
39             response = httpConn.getResponseCode();                 
40             if (response == HttpURLConnection.HTTP_OK) {
41                 in = httpConn.getInputStream();                                 
42             }                     
43         }
44         catch (Exception ex)
45         {
46             Log.d("Networking", ex.getLocalizedMessage());
47             throw new IOException("Error connecting");
48         }
49         return in;     
50     }
51     
52     private Bitmap DownloadImage(String URL)
53     {        
54         Bitmap bitmap = null;
55         InputStream in = null;        
56         try {
57             in = OpenHttpConnection(URL);
58             bitmap = BitmapFactory.decodeStream(in);
59             in.close();
60         } catch (IOException e1) {
61             Log.d("NetworkingActivity", e1.getLocalizedMessage());            
62         }
63         return bitmap;                
64     }
65         
66 
67     private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
68         protected Bitmap doInBackground(String... urls) {
69             return DownloadImage(urls[0]);
70         }
71         
72         protected void onPostExecute(Bitmap result) {
73             ImageView img = (ImageView) findViewById(R.id.img);
74             img.setImageBitmap(result);
75         }
76     }
77 
78 @Override
79     public void onCreate(Bundle savedInstanceState) {
80         super.onCreate(savedInstanceState);
81         setContentView(R.layout.main);
82         
83         new DownloadImageTask().execute("http://www.mayoff.com/5-01cablecarDCP01934.jpg");
84 }
85 }

因为DownloadImage()方法是同步的——这意味着,在图片下载完之前不会返回控制权——所以直接调用它会导致活动UI被冻结,使用AsyncTask允许在单独的线程中执行后台任务,然后在UI线程中返回结果。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。