android读取图片
来源:互联网 发布:80s下载软件 编辑:程序博客网 时间:2024/06/09 18:45
一:读取res中的图片
//读取本地res中的图片public static Bitmap readBitmap(int resid){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
//获取资源图片
InputStream input = getResources().openRawResource(resid);
return BitmapFactory.decodeStream(input,null,opt);
}
//确定不再需要该bitmap对象的时候可以将其回收掉
public static void recycle(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");
四:读取网络图片
public static Bitmap loadImageFromUrl(String url){
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
- android读取网络图片
- android读取图片
- android读取图片
- android读取图片
- android读取网络图片
- android读取图片
- Android读取网络图片
- Android 图片文件读取
- Android读取服务器图片
- Android 读取Assets中图片
- Android 读取Assets中图片
- Android 读取Assets中图片
- Android 读取Assets中图片
- android读取远程图片案例
- 读取图片 优化android内存
- android Bitmap 读取缩放图片
- Android 读取Assets中图片
- Android 读取图片方向信息
- IT English Collection(15)of Outlet
- launcher中Workspace类大致分析
- 详解not in与not exists的区别与用法(not in的性能并不差!)
- 一条sql语句实现统计查询
- sqlserver中with(nolock)深入分析
- android读取图片
- 领域模型之分层
- mem_fun和boost::Bind使用
- linux防火墙设置相关
- 图像、帧、片、NALU(转载)
- 当 MySQL 和 Memcached 遇到尾部空格时
- c#实现对登陆信息的反馈,实现对网站登录密码的扫描
- 先说一下本文中会提到的内容:##,__VA_ARGS__, __FILE__, __LINE__ , __FUNCTION__等
- HTML5 History学习