1. 程式人生 > >Android中從網上下載一張圖片顯示進度並顯示下載好的圖片

Android中從網上下載一張圖片顯示進度並顯示下載好的圖片

前幾天在網上看到個網上面試的題,需要完成一段程式碼,從網路(用底層URlConnection)上下載一個圖片並顯示進度。

然後總結了一下一個簡單的小Demo,跳轉即可執行的哦,親測有效,供大家參考:

1.主介面:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import 
android.widget.ImageView; import android.widget.SeekBar; import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class DownLoadActivity extends AppCompatActivity { private SeekBar mSeekBar
; private final String urlString = "http://f.hiphotos.baidu.com/image/pic/item/b7fd5266d016092446517fdadd0735fae7cd34ff.jpg"; private ImageView mIV; private int fileLength; private Bitmap mBitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_down_load
); mSeekBar = (SeekBar)findViewById(R.id.seekbar); mIV = (ImageView)findViewById(R.id.image); new Thread(downloadRunn).start(); } private Runnable downloadRunn = new Runnable(){ @Override public void run() { try { URL url = new URL(urlString); URLConnection connection = url.openConnection(); connection.connect(); fileLength = connection.getContentLength(); InputStream in = new BufferedInputStream(connection.getInputStream()); byte[] arr = readStream(in); mBitmap = BitmapFactory.decodeByteArray(arr,0,arr.length); connectHanlder.sendEmptyMessage(0); } catch (Exception e) { e.printStackTrace(); } } }; public byte[] readStream(InputStream in) throws Exception{ ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte data[] = new byte[1024]; long total = 0; int count; while((count = in.read(data))!=-1){ total+=count; mSeekBar.setProgress((int)(total*100)/fileLength); bos.write(data,0,count); } bos.close(); in.close(); return bos.toByteArray(); } private Handler connectHanlder = new Handler() { @Override public void handleMessage(Message msg) { // 更新UI,顯示圖片 if (mBitmap != null) { mIV.setImageBitmap(mBitmap);// display image } } }; }

2.佈局檔案中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
tools:context="com.othershe.test.DownLoadActivity">

    <SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="50"/>
    <ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="center"/>
</LinearLayout>
3.清單檔案不要忘記新增許可權哦;
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
如有問題,請在下方評論欄留言,或者新增QQ:1119348287(微信同步)。