我选择图片时BitmapFactory.decodeFile的问题

时间:2017-02-17 11:00:07

标签: android android-studio

当我选择图片时,BitmapFactory.decodeFile返回null。 这是我的功能:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // Here we need to check if the activity that was triggers was the Image Gallery.
    // If it is the requestCode will match the LOAD_IMAGE_RESULTS value.
    // If the resultCode is RESULT_OK and there is some data we know that an image was picked.
    if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && data != null) {
        // Let's read picked image data - its URI
        Uri pickedImage = data.getData();
        // Let's read picked image path using content resolver
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();
        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath,options);
        //imageView.setImageBitmap(bitmap);

        // Do something with the bitmap


        // At the end remember to close the cursor or you will end with the RuntimeException!
        cursor.close();
    }

}

有人可以告诉我这是什么问题吗?

1 个答案:

答案 0 :(得分:0)

String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));

你在变量 imagePath 中有filePath吗?或者imagePath值是否也为空?

更新
你的 BitmapFactory.decodeFile()返回null,因为可能是imageSize太大而且它抛出了一些异常。
检查你的日志,看看是否有任何 outOfMemory Bitmap 错误。让我知道