Android 读取Bitmap的几种方式
来源:互联网 发布:生死狙击狸猫js直播间 编辑:程序博客网 时间:2024/06/06 07:35
想读取本地项目里的资源图片,但又不能用到R文件。查了很多资料终于找到了。
现总结以下几种读取Bitmap的方法。
1.以文件流的方式,假设在sdcard下有 test.png图片
FileInputStream fis = new FileInputStream("/sdcard/test.png");
Bitmap bitmap = BitmapFactory.decodeStream(fis);
2. 以R文件的方式,假设 res/drawable下有 test.jpg文件
Bitmap bitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test);
3.以ResourceStream的方式,但不用到R文件。
Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(“/res/drawable/test.png”));
说明一下,我为什么用第三种方法读取Bitmap。
因为项目的要求啦,哈哈!废话。主要是方便把程序的资源图片插入到本地sqlite中,到时只需传入/res/drawable + 图片名就可以读取到 Bitmap啦。
还有一种情况值得考虑的,就是当图片资源太大的适合,会出现内存溢出。如何解决呢?
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; //图片宽高都为原来的二分之一,即图片为原来的四分一
//以上代码可以优化内存溢出,但它只是改变图片大小,并不能彻底解决内存溢出。
- Android 读取Bitmap的几种方式
- android 读取Bitmap的几种方式
- android 读取Bitmap的几种方式
- android 读取文件生成Bitmap的几种方式
- 读取Bitmap的几种方式
- Android 获取Bitmap的几种方式
- android 生成 Bitmap 的常用几种方式
- Android传递Bitmap的几种简单方式
- 【Android】获取Bitmap的几种方式总结
- android 获得bitmap 几种方式
- andorid小记读取Bitmap 的几种方式和优化内存溢出
- 获取bitmap的几种方式
- 获取Bitmap的几种方式总结
- Bitmap压缩的几种方式
- 安卓获得Bitmap的几种方式
- android获取Bitmap的两种方式
- properties文件读取的几种方式
- Properties 文件 读取 的几种方式
- 关于Android file.createNewFile() 方法出现的问题总结
- UML类图几种关系的总结
- 关于项目经理不懂技术
- 根据进程名获得进程ID(不区分进程名大小写)
- 深入学习Tomcat——servlet容器
- Android 读取Bitmap的几种方式
- [全程建模]用例、用例图和用例模型的概念解析
- 2008-数据库初接触
- Drawable、Bitmap、byte[]之间的转换 Bitmap转换成InputStream。
- poj3694 Network 双连通分量、并查集
- Drools Flow(jbpm5学习资料)
- android异步回调加载网络图片
- 设计模式之一:单例模式
- C++引用详解(一)