• 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线程中返回结果。

  • 相关阅读:
    抽象类存在的意义
    抽象类的特征
    抽象类的使用
    抽象类的概述
    引用类型作为方法参数和返回值
    继承的特点
    目前Java水平以及理解自我反思---01
    继承后- 构造器的特点
    指针函数
    C数组灵活多变的访问形式
  • 原文地址:https://www.cnblogs.com/hixin/p/4106640.html
Copyright © 2020-2023  润新知