前几天在网上看到个网上面试的题,需要完成一段代码,从网络(用底层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(微信同步)。