【问题标题】:BitmapFactory.decodeFile returns exception on Android 4.4 KitKatBitmapFactory.decodeFile 在 Android 4.4 KitKat 上返回异常
【发布时间】: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

标签: android android-bitmap


【解决方案1】:

您可以尝试将以下内容放入清单文件中:

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

【讨论】:

  • 非常感谢!我无法相信解决方案如此简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多