FileNotFoundException:打开失败:EACCES(权限被拒绝) [英] FileNotFoundException: open failed: EACCES (Permission denied)
问题描述
在模拟器(我使用genymotion)中,它工作正常,但是当我在真实设备(我的手机是ASUS ZenFone Laser 5.0)上运行它时,会抛出filenotfoundexception
In emulator (i use genymotion) it works fine, but when I run it on a real device (my phone is ASUS ZenFone Laser 5.0) throws a filenotfoundexception
java.io.FileNotFoundException:/storage/emulated/0/cam20160926_075819.jpg:打开失败:EACCES(权限被拒绝)
imgBitmap = MediaStore.Images.Media.getBitmap(cr,selectedImage);
imgBitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
这是onActivityResult()方法
here's the method onActivityResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case CAMERA_REQUEST:
if (resultCode == Activity.RESULT_OK){
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap imgBitmap;
try {
imgBitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
accountPhoto.setImageBitmap(imgBitmap);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(), "Something went wrong while taking a photo", Toast.LENGTH_LONG).show();
Log.e("Camera", e.toString());
}
}
}
}
我阅读了有关此EACCES的一些相关问题和解决方案,看来问题是在我的允许下进行的:
i read some related questions and solutions about this EACCES, and it seems the problem is on my permission:
<uses-feature android:name="android.hardware.camera2" android:required="true"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
我错过了什么吗?感谢您的答复
am I missing something? thanks for responding
推荐答案
在开始使用说明之前,您必须先请求权限,因为在棉花糖中此权限被认为是危险的:
you have to request permission before you start your instructions because this permission is considered dangerous in Marshmallow:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RESULT);
}
else
{
//your code
}
这篇关于FileNotFoundException:打开失败:EACCES(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!