Android读取本地或者网络图片的方法
16lz
2021-01-26
- /**
- *得到本地或者网络上的bitmapurl-网络或者本地图片的绝对路径,比如:
- *
- *A.网络路径:url="http://blog.foreverlove.us/girl2.png";
- *
- *B.本地路径:url="file://mnt/sdcard/photo/image.png";
- *
- *C.支持的图片格式,png,jpg,bmp,gif等等
- *
- *@paramurl
- *@return
- */
- publicstaticBitmapGetLocalOrNetBitmap(Stringurl)
- {
- Bitmapbitmap=null;
- InputStreamin=null;
- BufferedOutputStreamout=null;
- try
- {
- in=newBufferedInputStream(newURL(url).openStream(),Constant.IO_BUFFER_SIZE);
- finalByteArrayOutputStreamdataStream=newByteArrayOutputStream();
- out=newBufferedOutputStream(dataStream,Constant.IO_BUFFER_SIZE);
- copy(in,out);
- out.flush();
- byte[]data=dataStream.toByteArray();
- bitmap=BitmapFactory.decodeByteArray(data,0,data.length);
- data=null;
- returnbitmap;
- }
- catch(IOExceptione)
- {
- e.printStackTrace();
- returnnull;
- }
- }
说明:Constant.IO_BUFFER_SIZE 是一个常量而已,可以改成常数,比如2*1024,其实取决于你的图片大小,自己根据图片的大小自己设定吧。。。。
更多相关文章
- android wifi讲解 wifi列表显示
- Android网络安全之NetworkSecurityConfig
- 学习Android之SimpleAdapter显示网络图片
- 请求对象Android(安卓)网络操作常用的两个类
- Android学习09-----Android中数据的存储和访问 (1) By SharedPre
- CCSprite在Android平台下显示SDCard中的图片
- Android(安卓)网络框架学习之OKHttp
- Android(安卓)Studio 那些事| ' ' cannot be applied to '(groovy
- Android(安卓)开发中遇到的 Exception & ANR