第一篇博客——BitmapFactory Unable to decode stream 问题解决
来源:互联网 发布:淘宝巴拉巴拉羽绒服 编辑:程序博客网 时间:2024/05/21 07:14
BitmapFactory Unable to decode stream 问题解决
第一篇博客,记录下遇到的错误吧。
今天做项目时从手机根目录读取图片文件并显示在ImageView中,习惯性的写下如下代码
File picture = new File(Environment.getExternalStorageDirectory(), "test.jpg");
String filepath = Uri.fromFile(picture).toString();
Bitmap bitmap = BitmapFactory.decodeFile( filepath);
运行时后提示错误BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: file:/storage/emulated/0/test.jpg: open failed: ENOENT (No such file or directory
查阅资料后发现并没有获取路径。
第一种修改:
File picture = new File(Environment.getExternalStorageDirectory(), "test.jpg");
Uri filepath = Uri.fromFile(picture);
Bitmap bitmap = BitmapFactory.decodeFile(filepath.getPath());
第二种修改:
bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(filepath));
通过获取数据流的方式比较安全。
从Android7.0后,直接使用本地真是Uri被认为是不安全的,会抛出一个FileUriExposedException异常。所以应该加个判断
if (Build.VERSION.SDK_INT < 24) { imageUri = Uri.fromFile(picture); } else { imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.cameraalbumtest.fileprovider", picture); }
getUriForFile()接受3个参数,第一个参数要求传入Context对象,第二个可以是任意的唯一的字符串,第三个参数则是刚刚创建的file对象。
FileProvider是一种特殊的内容提供器,使用了和内容提供器类似的机制来对数据进行保护,可以选择性的将封装的Uri共享给外部,从而提高应用的安全性。
参考资料:http://stackoverflow.com/questions/21195899/bitmapfactory-unable-to-decode-stream
郭神的《第一行代码》
下一篇应该会写下retrofit2的初体验以及一些遇到的问题和解决方法。
- 第一篇博客——BitmapFactory Unable to decode stream 问题解决
- Unable to decode stream: java.io.FileNotFoundException: 虎头:
- 小昆哥—>博客第一篇
- Unable to decode stream: java.io.FileNotFoundException: /android.graphics.Bitmap open failed: ENOEN
- 博客更新——第一篇
- 第一篇博客——maven笔记
- 第一篇博客——个人感悟
- 第一篇博客——快速排序
- matlab aviread Unable to locate decompressor to decompress video stream(问题解决最佳方案)
- 第一篇博客—处女贴
- 第一篇博客—markdown入门
- Unable to recognize OLE stream
- 第一篇博客——写博客的目的
- To Be a Geek从第一篇博客开始
- 建立博客第一篇技术文章——奋斗
- 狂龙的第一篇博客——好友必读!
- 我的第一篇博客——宣言
- javascript模拟时钟 ——我的第一篇博客
- C++离航篇——函数默认参数、函数重载、内敛函数
- JAVA基础之循环语句(1)
- some tricks about 2s complementary about mask address
- Java常见异常总结
- LightOJ1030- Discovering Gold(期望dp)
- 第一篇博客——BitmapFactory Unable to decode stream 问题解决
- ubuntu hosts
- 浅谈jQuery源码(六)——$.map
- Java使用Comparable接口自定义排序
- 【Android 一般进阶】动态广播注册注销时机
- 后缀数组(suffix array) 模板(1)
- spring(一)---bean的学习上篇
- 包装类和日期操作
- Java 数组去重复值