国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現下載圖片及在手機中展示的方法

2019-12-12 03:39:17
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現下載圖片及在手機中展示的方法。分享給大家供大家參考,具體如下:

在項目開發中從互聯網上下載圖片是經常用到的功能,再次總結一下

1.普通的下載方式

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:src="@drawable/icon"   android:layout_width="wrap_content"   android:id="@+id/imgPic"   android:layout_gravity="center|center_vertical"   android:layout_height="fill_parent"> </ImageView></LinearLayout>

java文件

public class DownloadImage extends Activity {  private ImageView imgPic;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.download_image);    imgPic = (ImageView) findViewById(R.id.imgPic);    String url = "http://ww1.sinaimg.cn/bmiddle/6834c769jw1djjf4p3p9rj.jpg";    loadRmoteImage(url);  }  /**   * @param imgUrl   *   遠程圖片文件的URL   *   *   下載遠程圖片   */  private void loadRmoteImage(String imgUrl) {    URL fileURL = null;    Bitmap bitmap = null;    try {      fileURL = new URL(imgUrl);    } catch (MalformedURLException err) {      err.printStackTrace();    }    try {      HttpURLConnection conn = (HttpURLConnection) fileURL          .openConnection();      conn.setDoInput(true);      conn.connect();      InputStream is = conn.getInputStream();      int length = (int) conn.getContentLength();      if (length != -1) {        byte[] imgData = new byte[length];        byte[] buffer = new byte[512];        int readLen = 0;        int destPos = 0;        while ((readLen = is.read(buffer)) > 0) {          System.arraycopy(buffer, 0, imgData, destPos, readLen);          destPos += readLen;        }        bitmap = BitmapFactory.decodeByteArray(imgData, 0,            imgData.length);      }    } catch (IOException e) {      e.printStackTrace();    }    imgPic.setImageBitmap(bitmap);  }

2.帶進度條的下載

有時候網絡差,或者是圖片太大,會出現黑屏的情況,用戶體驗比較差,那么增加一個進度條是提高用戶體驗的好方法

/** * @author xushilin xsl xushilin@kingtoneinfo.com * @version: 創建時間:2011-7-27 下午02:55:56 * 說 明: android中下載圖片 * 修改歷史: */public class DownloadImage extends Activity {  private ImageView imgPic;  private ProgressBar progressBar;  private int totalSize=0;  private int size=0;  private Handler mHandler;  String url = "http://ww1.sinaimg.cn/bmiddle/6834c769jw1djjf4p3p9rj.jpg";  private Bitmap bitmap=null;  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.download_image);    imgPic = (ImageView) findViewById(R.id.imgPic);    progressBar = (ProgressBar) findViewById(R.id.progressBar);    progressBar.setProgress(getProgressInt(progressBar.getMax()));    mHandler = new Handler() {      public void handleMessage(Message msg) {        progressBar.setProgress(getProgressInt(progressBar.getMax()));        if(bitmap!=null){          imgPic.setImageBitmap(bitmap);        }      }    };    new Thread(){      public void run(){        loadRmoteImage(url);      }    }.start();  }  /**   * @param imgUrl   *   遠程圖片文件的URL   *   *   下載遠程圖片   */  private void loadRmoteImage(String imgUrl) {    URL fileURL = null;    try {      fileURL = new URL(imgUrl);    } catch (MalformedURLException err) {      err.printStackTrace();    }    try {      HttpURLConnection conn = (HttpURLConnection) fileURL          .openConnection();      conn.setDoInput(true);      conn.connect();      InputStream is = conn.getInputStream();      int length = (int) conn.getContentLength();      totalSize=length;      if (length != -1) {        byte[] imgData = new byte[length];        byte[] buffer = new byte[512];        int readLen = 0;        int destPos = 0;        while ((readLen = is.read(buffer)) > 0) {          System.arraycopy(buffer, 0, imgData, destPos, readLen);          destPos += readLen;          size=destPos;          mHandler.sendEmptyMessage(1);          Thread.sleep(100);        }        bitmap = BitmapFactory.decodeByteArray(imgData, 0,            imgData.length);        mHandler.sendEmptyMessage(1);      }    } catch (IOException e) {      e.printStackTrace();    } catch (InterruptedException e) {      e.printStackTrace();    }  }  private int getProgressInt(int max) {    int result = (totalSize > 0) ? (int) (size * max * 1.0 / totalSize) : 0;    return result;  }}

效果如下:

下載過程:

下載完成:

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結》、《Android開發入門與進階教程》、《Android調試技巧與常見問題解決方法匯總》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

希望本文所述對大家Android程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桦川县| 黑河市| 玉林市| 依安县| 玉环县| 剑川县| 萨嘎县| 德江县| 通榆县| 綦江县| 海盐县| 油尖旺区| 郑州市| 积石山| 治县。| 车致| 满洲里市| 长垣县| 贵溪市| 辽源市| 卢氏县| 寻乌县| 富阳市| 托里县| 洛川县| 武功县| 宜宾县| 许昌县| 三原县| 友谊县| 新晃| 松溪县| 芦山县| 来宾市| 和静县| 中阳县| 临西县| 西林县| 平泉县| 清丰县| 绵阳市|