溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)中怎么實(shí)現(xiàn)一個(gè)圖片下載功能

發(fā)布時(shí)間:2020-11-24 15:22:35 來源:億速云 閱讀:163 作者:Leah 欄目:移動(dòng)開發(fā)

本篇文章給大家分享的是有關(guān)Android開發(fā)中怎么實(shí)現(xiàn)一個(gè)圖片下載功能,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

1.普通的下載方式

布局文件:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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
   *   遠(yuǎn)程圖片文件的URL
   *
   *   下載遠(yuǎn)程圖片
   */
  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.帶進(jìn)度條的下載

有時(shí)候網(wǎng)絡(luò)差,或者是圖片太大,會(huì)出現(xiàn)黑屏的情況,用戶體驗(yàn)比較差,那么增加一個(gè)進(jìn)度條是提高用戶體驗(yàn)的好方法

/**
 * @author xushilin xsl xushilin@kingtoneinfo.com
 * @version: 創(chuàng)建時(shí)間: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
   *   遠(yuǎn)程圖片文件的URL
   *
   *   下載遠(yuǎn)程圖片
   */
  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) &#63; (int) (size * max * 1.0 / totalSize) : 0;
    return result;
  }
}

以上就是Android開發(fā)中怎么實(shí)現(xiàn)一個(gè)圖片下載功能,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI