44 Android各种地方图片读取(加载) - 雪炭网

Android各种地方图片读取(加载)2014-10-24 16:39:25

( 4人已投票,[高质量] )
分享:
31.3K

Android加载图片是很常见的操作,下面就是如何读取res资源中的图片、读取assets内的图片、读取sd卡内的图片及网络图片的读取


一:读取res中的图片

//读取本地res中的图片
publicstaticBitmap readBitmap(intresid){
BitmapFactory.Options opt = newBitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
InputStream input = getResources().openRawResource(resid);
returnBitmapFactory.decodeStream(input,null,opt);
}
//回收资源(很重要)
publicstaticvoidrecycle(Bitmap bitmap){
if(bitmap!=null && !bitmap.isRecycle()){
bitmap.recycle();
System.gc();//及时回收
}
}


二:放在assets中的图片

InputStream is = context.getResources().getAssets().open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(is);


三:读取sd卡中的图片

Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/icon.png");


四:读取网络图片

publicstaticBitmap loadImageFromUrl(String urlStr){
URL url;
InputStream i = null;
try{
m = newURL(urlStr);
i = m.getContent();
}catch(Exception e){
e.printStaceTrace();
}
returnBitmapFactory.decodeStream(i);
}


以上为自种图片加载方式,喜欢就收藏吧~





头像

snowcoal
  • Android
  • 加载图片

本文标签:

Android加载图片

收藏到我的私密空间

标题:Android各种地方图片读取(加载)

作者:花花世界

你暂未登录,请登录后才可收藏至您的私密空间 确认取消
雪炭网

键盘操作 更便捷 -雪炭网雪中送炭-乐趣无限

如果本站的内容有幸帮助到了您,建议您了解一下当页的广告内容哦,我们的进步离不开您的支持,Thank you~