【发布时间】:2013-12-21 19:51:12
【问题描述】:
我想使用以下代码显示图像:
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.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 filePath = cursor.getString(columnIndex);
cursor.close();
Log.wtf("M3K", "Above decode");
Bitmap logoBMP = BitmapFactory.decodeFile(filePath);
Log.wtf("M3K", "Below decode");
//Display image on layout
Log.wtf("M3K", "Above display");
logo.setImageBitmap(logoBMP);
Log.wtf("M3K", "Below display");
}
}
}
问题出在Bitmap logoBMP = BitmapFactory.decodeFile(filePath); 上,在 Android 4.4(在我的 Nexus 7 上测试)上,它会返回一个文件未找到异常,原因是 EACCES(权限被拒绝)。这在运行 4.2 的 ASUS Transformer Infinity 上完美运行,并在运行 4.3 的 Nexus 7 上完美运行。有谁知道 KitKat 兼容性需要改变什么?
注意:我通过以下代码获取图片URI:
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
【问题讨论】:
-
permission denied 的意思,尝试给WRITE_EXTERNAL_STORAGE