我遇到有关BitMapFactory.decodeFile的问题。
在我的应用中,我希望用户能够从他/她的设备中选择图像或拍照。然后必须在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)
如何解决它。请帮助我。谢谢。
答案 0 :(得分:16)
它的权限问题,您需要在清单中为外部读取存储添加权限,然后才能使用它,如果您使用的是6.0以上,则需要使用Easy权限。
写作:
colors=np.random.randint(len(dist))
cmap=plt.cm.viridis
plt.scatter(dist,corr,c=colors,cmap=cmap)
plt.colorbar()
plt.show()
阅读:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
6.0以上:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 1 :(得分:1)
启动CAMERA意图时,您已经要求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;
}
处理权限对话框&#34;允许&#34;和&#34;拒绝&#34; 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;
}
}
}
并将其添加到您的Manifest应用程序代码中:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 2 :(得分:0)
将此添加到您的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();
}
}
}