Android:加载图像 --> 打开失败:EACCES
Android : Load Image --> open failed : EACCES
Android 6.0 - 棉花糖
我只想打开图库并选择一张图片,但出现错误:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Download/my_image-1.png: open failed: EACCES (Permission denied)
这是我在清单中的权限声明:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这是我的图片选择器:
((Button)view.findViewById(R.id.button_Logo)).setOnClickListener(new View.OnClickListener(){
@Override
public void OnClick(View v) {
Intent getIntent = new Intent(Intent.ACTION_GETCONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getInten, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooseIntent, PICK_IMAGE);
}
});
在清单中添加以下两个权限
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_EXTERNAL_STORAGE
好的,请从您的意图中删除此行 "getintent.setType(images/*)"。
并做出这样的单一意图...
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(i,
"Select Picture"), PICK_IMAGE_REQUEST);
对于 API 23+ 您需要请求 read/write 权限,即使它们已经在您的清单中。
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
Android 6.0 - 棉花糖
我只想打开图库并选择一张图片,但出现错误:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Download/my_image-1.png: open failed: EACCES (Permission denied)
这是我在清单中的权限声明:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这是我的图片选择器:
((Button)view.findViewById(R.id.button_Logo)).setOnClickListener(new View.OnClickListener(){
@Override
public void OnClick(View v) {
Intent getIntent = new Intent(Intent.ACTION_GETCONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getInten, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooseIntent, PICK_IMAGE);
}
});
在清单中添加以下两个权限
android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STORAGE
好的,请从您的意图中删除此行 "getintent.setType(images/*)"。 并做出这样的单一意图...
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(i,
"Select Picture"), PICK_IMAGE_REQUEST);
对于 API 23+ 您需要请求 read/write 权限,即使它们已经在您的清单中。
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}