BitmapFactory:无法解码流:java.io.FileNotFoundException
Posted
技术标签:
【中文标题】BitmapFactory:无法解码流:java.io.FileNotFoundException【英文标题】:BitmapFactory: Unable to decode stream: java.io.FileNotFoundException 【发布时间】:2017-07-23 03:09:14 【问题描述】:我有一个关于 BitMapFactory.decodeFile 的问题。
在我的应用中,我希望用户能够从他/她的设备中选择图像或拍照。然后必须将其显示在 ImageView 中
这里是代码sn-p:
Uri selectedImage = data.getData();
String[] filePathColumn = MediaStore.Images.Media.DATA;
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
MyImage image = new MyImage();
image.setTitle("Test");
image.setDescription("test choose a photo from gallery and add it to " + "list view");
image.setDatetime(System.currentTimeMillis());
image.setPath(picturePath);
我得到了这个例外:
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170302-WA0012.jpg: open failed: EACCES (Permission denied)
如何解决。请帮助我。提前谢谢..
【问题讨论】:
对于android Q,请参见下面的解决方案:***.com/a/57804657/1015686 【参考方案1】:它的权限问题,你需要在manifest中添加外部读取存储的权限,然后才能使用它,如果你使用的是6.0以上的操作系统,那么你需要使用Easy权限。
写:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
供阅读:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
6.0以上:
private String[] galleryPermissions = Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (EasyPermissions.hasPermissions(this, galleryPermissions))
pickImageFromGallery();
else
EasyPermissions.requestPermissions(this, "Access for storage",
101, galleryPermissions);
【讨论】:
如果是类或字段,这个 EasyPermissions 是什么?? 它的库编译'pub.devrel:easypermissions:0.2.1' 如果您不想使用 EasyPermissions 库,只需遵循以下答案:***.com/a/39866945/8407719【参考方案2】:当您启动 CAMERA Intent 时,您已请求 READ-EXTERNAL 存储的运行时权限:
final int MyVersion = Build.VERSION.SDK_INT;
if (MyVersion > Build.VERSION_CODES.LOLLIPOP_MR1)
if (!checkIfAlreadyhavePermission())
ActivityCompat.requestPermissions(new String[]android.Manifest.permission.READ_EXTERNAL_STORAGE, 1);
else
startYourCameraIntent();
checkIfAlreadyhavePermission() 方法:
private boolean checkIfAlreadyhavePermission()
int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
在 onRequestPermission() 中处理权限对话框“允许”和“拒绝”按钮操作:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
case 1:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED)
startYourCameraIntent();
else
Toast.makeText(getActivity(), "Please give your permission.", Toast.LENGTH_LONG).show();
break;
并将其添加到您的清单的应用程序标签中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
【讨论】:
在你的活动课上。【参考方案3】:将此添加到您的 MainActivity.java 文件中
6.0 以上操作系统:
if(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this,new String[]
Manifest.permission.READ_EXTERNAL_STORAGE
,1);
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==1)
if(grantResults[0]==PackageManager.PERMISSION_GRANTED)
else
Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_LONG).show();
【讨论】:
以上是关于BitmapFactory:无法解码流:java.io.FileNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章
BitmapFactory:无法解码流:java.io.FileNotFoundException:打开失败:Android Q 上的 EACCES(权限被拒绝)
BitmapFactory.decodeStream返回null的 ,InputStream 被调用两次,第一次调用流被关闭清空了!!!