在模拟器中(我使用genymotion),它可以正常工作,但是当我在真实设备(我的手机是ASUS ZenFone Laser 5.0)上运行它时,会抛出一个文件notfoundexception
java.io.FileNotFoundException:/storage/emulated/0/cam20160926_075819.jpg:打开失败:EACCES(权限被拒绝)
这是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的一些相关问题和解决方案,似乎该问题已得到我的许可:
<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" />
我错过了什么吗?谢谢你的回应
最佳答案
您必须先获得许可才能开始使用说明,因为在棉花糖中此许可被认为是危险的:
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
}
关于java - FileNotFoundException : open failed: EACCES (Permission denied),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39693082/