Android網路下載圖片

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   ar   color   os   

 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 IOException24     {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 @Override79     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線程中返回結果。

Android網路下載圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.