Android编程实现下载图片及在手机中展示的方法
程序员文章站
2023-12-19 08:14:16
本文实例讲述了android编程实现下载图片及在手机中展示的方法。分享给大家供大家参考,具体如下:
在项目开发中从互联网上下载图片是经常用到的功能,再次总结一下
1.普...
本文实例讲述了android编程实现下载图片及在手机中展示的方法。分享给大家供大家参考,具体如下:
在项目开发中从互联网上下载图片是经常用到的功能,再次总结一下
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.带进度条的下载
有时候网络差,或者是图片太大,会出现黑屏的情况,用户体验比较差,那么增加一个进度条是提高用户体验的好方法
/** * @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; @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; } }
效果如下:
下载过程:
下载完成:
更多关于android相关内容感兴趣的读者可查看本站专题:《android图形与图像处理技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。