BitmapFactory:无法解码流:java.io.FileNotFoundException
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException
我有关于 BitMapFactory.decodeFile 的问题。
在我的应用中,我希望用户能够从 his/her 设备 select 图片或拍照。然后必须将其显示在 ImageView
中
这是代码片段:
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)
如何解决it.Please求助me.Thanks提前..
它的权限问题,你需要在manifest中添加外部读取存储的权限,然后才能使用它,如果你使用os 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);
}
当您启动 CAMERA intent 时,您已请求 运行 次读取外部存储的权限:
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() 中处理权限对话框 "Allow" 和 "Deny" 按钮操作:
@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" />
将此添加到您的 MainActivity.java 文件中
6.0 以上OS:
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.decodeFile 的问题。
在我的应用中,我希望用户能够从 his/her 设备 select 图片或拍照。然后必须将其显示在 ImageView
中这是代码片段:
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)
如何解决it.Please求助me.Thanks提前..
它的权限问题,你需要在manifest中添加外部读取存储的权限,然后才能使用它,如果你使用os 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);
}
当您启动 CAMERA intent 时,您已请求 运行 次读取外部存储的权限:
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() 中处理权限对话框 "Allow" 和 "Deny" 按钮操作:
@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" />
将此添加到您的 MainActivity.java 文件中
6.0 以上OS:
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();
}
}
}