Android中从网上下载一张图片显示进度并显示下载好的图片
来源:互联网 发布:Mac打不了大写英文字母 编辑:程序博客网 时间:2024/04/30 02:18
前几天在网上看到个网上面试的题,需要完成一段代码,从网络(用底层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(微信同步)。
阅读全文
0 0
- Android中从网上下载一张图片显示进度并显示下载好的图片
- Android 从网上下载图片并显示到ListView上
- iOS下载图片并显示进度
- AsyncTask下载网络图片并显示下载进度
- AsyncTask下载图片、显示进度
- 实现从网上下载大量图片在GridView上显示过程中遇到的一些问题
- android 从网页上下载图片并显示出来.
- 运用AsyncTask下载图片并显示下载进度,下载完成后显示
- LASIImageView - 显示进度指示并异步下载图片
- android从网上下载图片
- Android从网上下载图片
- android中如何下载文件并显示下载进度
- android中如何下载文件并显示下载进度
- android下载网络图片并显示
- android 下载图片到本地并显示
- 安卓下载网络图片显示进度
- Android如何从图库获取一张图片并显示
- android网上下载图片存在本地并在Listview上显示
- java 换行符
- 树网的核(树的直径)
- 动态加载之后js失效
- android多线程以及线程池使用
- java.math.BigDecimal
- Android中从网上下载一张图片显示进度并显示下载好的图片
- 杂记
- 10月17日云栖精选夜读:什么是新零售?阿里巴巴最新的报告终于说清楚了!(附报告)
- HPE提供集合套件作为经营决策
- MySQL练习-employees数据库(一)
- git detached HEAD explained
- WFP过滤开发(一)
- postgresql cheatsheet
- Linux的运行级别和chkconfig用法