android下載圖片在手機中展示

來源:互聯網
上載者:User

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

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;@Overrideprotected 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;@Overrideprotected 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;}}

效果如下:

下載過程

下載完成:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.