最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法

 更新時(shí)間:2017年02月23日 09:04:40   作者:藍(lán)之風(fēng)  
這篇文章主要介紹了Android編程實(shí)現(xiàn)下載圖片及在手機(jī)中展示的方法,涉及Android針對(duì)圖形文件的遠(yuǎn)程下載及遍歷顯示相關(guān)操作技巧,需要的朋友可以參考下

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

在項(xiàng)目開(kāi)發(fā)中從互聯(lián)網(wǎng)上下載圖片是經(jīng)常用到的功能,再次總結(jié)一下

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
   *   遠(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
 * 說(shuō) 明: 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) ? (int) (size * max * 1.0 / totalSize) : 0;
    return result;
  }
}

效果如下:

下載過(guò)程:

下載完成:

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android圖形與圖像處理技巧總結(jié)》、《Android開(kāi)發(fā)入門(mén)與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

天门市| 亚东县| 渭南市| 柘城县| 富蕴县| 荆州市| 屏边| 东兰县| 揭阳市| 罗甸县| 鄂温| 象山县| 枣阳市| 建阳市| 通许县| 塘沽区| 象州县| 郯城县| 宝鸡市| 阆中市| 喀喇沁旗| 峨山| 浏阳市| 大埔区| 文登市| 平罗县| 望奎县| 松阳县| 永德县| 蓝山县| 洛南县| 无极县| 垣曲县| 临安市| 农安县| 唐河县| 特克斯县| 岐山县| 宣威市| 上林县| 多伦县|