欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

在項目開發(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
   *   遠程圖片文件的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.帶進度條的下載

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

/**
 * @author xushilin xsl xushilin@kingtoneinfo.com
 * @version: 創(chuàng)建時間: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;
  }
}

效果如下:

下載過程:

下載完成:

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

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

相關(guān)文章

  • Android獲取本地相冊圖片和拍照獲取圖片的實現(xiàn)方法

    Android獲取本地相冊圖片和拍照獲取圖片的實現(xiàn)方法

    這篇文章主要為大家詳細介紹了Android獲取本地相冊圖片和拍照獲取圖片的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android 開機直接運行app并當做手機桌面的實例

    Android 開機直接運行app并當做手機桌面的實例

    今天小編就為大家分享一篇Android 開機直接運行app并當做手機桌面的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • Android實現(xiàn)下載進度條效果

    Android實現(xiàn)下載進度條效果

    vivo商店在下載應(yīng)用的時候,底部有一個圓角矩形的下載進度條,中間有一個進度文字,而且進度和文字交匯的時候,交匯部分的文字會從藍色邊為白色,會有一種一半白色字,一半藍色字的效果。本文將仿照該樣式實現(xiàn)一個
    2021-06-06
  • Android自定義覆蓋層控件 懸浮窗控件

    Android自定義覆蓋層控件 懸浮窗控件

    這篇文章主要為大家詳細介紹了Android自定義覆蓋層控件和懸浮窗控件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • Android中基于XMPP協(xié)議實現(xiàn)IM聊天程序與多人聊天室

    Android中基于XMPP協(xié)議實現(xiàn)IM聊天程序與多人聊天室

    這篇文章主要介紹了Android中基于XMPP協(xié)議實現(xiàn)IM聊天程序與多人聊天室的方法,XMPP基于XML數(shù)據(jù)格式傳輸,一般用于即時消息(IM)以及在線現(xiàn)場探測,需要的朋友可以參考下
    2016-02-02
  • 詳解Android 裸眼3D效果View控件

    詳解Android 裸眼3D效果View控件

    主要的設(shè)計核心是依賴于傳感器對手機晃動的監(jiān)聽(重力感應(yīng)監(jiān)聽器),對每層圖片進行不同的移動,實現(xiàn)仿3D效果。本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧
    2021-08-08
  • Android 使用Kotlin自定義View的方法教程

    Android 使用Kotlin自定義View的方法教程

    最近想加強一下自定義view方面的學習,正好也在學習Kotlin,所以就嘗試著用Kotlin寫一下簡單的自定義view,下面這篇文章主要給大家介紹了關(guān)于Android使用Kotlin自定義View的方法教程,需要的朋友可以參考下。
    2017-12-12
  • Android 開發(fā)程序鎖應(yīng)用簡單實例

    Android 開發(fā)程序鎖應(yīng)用簡單實例

    這篇文章主要介紹了Android 開發(fā)程序鎖應(yīng)用簡單實例的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Android開發(fā)中Signal背后的bug與解決

    Android開發(fā)中Signal背后的bug與解決

    這篇文章主要為大家介紹了Android開發(fā)中Signal背后的bug與解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android 手機屏幕適配解決辦法

    Android 手機屏幕適配解決辦法

    這篇文章主要介紹了Android 手機屏幕適配的相關(guān)資料,在開發(fā)Android 手機開發(fā)的時候經(jīng)常會有很多手機品牌和手機屏幕尺寸問題,需要的朋友可以參考下
    2016-10-10

最新評論