java - BitmapFactory 无法解码流

标签 java android image

嘿,我不确定为什么每次我在图库中选择图片时都会出现这个问题?

代码如下:

if (v == uploadImageButton) {
                        // below allows you to open the phones gallery
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(
                                        Intent.createChooser(intent, "Complete action using"), 1);
                }


public void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (resultCode == RESULT_OK && requestCode == 1 && null != data) {
                        // Bitmap photo = (Bitmap) data.getData().getPath();
                        Uri selectedImageUri = data.getData();
                        String[] filePathColumn = { MediaStore.Images.Media.DATA };

                        Cursor cursor = getContentResolver().query(selectedImageUri,
                                        filePathColumn, null, null, null);
                        cursor.moveToFirst();

                        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                        String picturePath = cursor.getString(columnIndex);
                        cursor.close();
                        Log.e("Picture", picturePath);
                        decodeFile(picturePath);
                }
        }


public void decodeFile(String filePath) {
                // TODO Auto-generated method stub
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeFile(filePath, o);

                // the new size we want to scale to
                final int REQUIRED_SIZE = 1024;

                // Find the correct scale value. It should be the power of 2.
                int width_tmp = o.outWidth, height_tmp = o.outHeight;
                int scale = 1;
                while (true) {
                        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                                break;
                        }
                        width_tmp /= 2;
                        height_tmp /= 2;
                        scale *= 2;
                }

                // decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);

                imageview.setImageBitmap(bitmap);
        }

错误:

01-17 23:18:04.642: D/Documents(25157): onFinished() [content://com.android.providers.media.documents/document/image%3A901]
01-17 23:18:04.682: E/BitmapFactory(24993): Unable to decode stream: java.lang.NullPointerException
01-17 23:18:04.682: E/BitmapFactory(24993): Unable to decode stream: java.lang.NullPointerException
01-17 23:18:09.732: I/InputReader(766): Reconfiguring input devices.  changes=0x00000004
01-17 23:18:09.732: I/InputReader(766): Device reconfigured: id=4, name='touch_dev', size 1080x1920, orientation 3, mode 1, display id 0

最佳答案

不要假设有文件路径。 Android 4.4 及更高版本即将删除它们。而你得到的uri已经没有路径了。

您仍然可以通过 InputStream (ContentResolver#openInputStream(Uri uri)) 或通过文件描述符访问文件内容。

这里有解释:ContentProviders: Open a document (向下滚动,链接到部分似乎已损坏)

这也适用于较旧的 android 版本。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK && requestCode == 1 && null != data) {
        decodeUri(data.getData());
    }
}

public void decodeUri(Uri uri) {
    ParcelFileDescriptor parcelFD = null;
    try {
        parcelFD = getContentResolver().openFileDescriptor(uri, "r");
        FileDescriptor imageSource = parcelFD.getFileDescriptor();

        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(imageSource, null, o);

        // the new size we want to scale to
        final int REQUIRED_SIZE = 1024;

        // Find the correct scale value. It should be the power of 2.
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2);

        imageview.setImageBitmap(bitmap);

    } catch (FileNotFoundException e) {
        // handle errors
    } catch (IOException e) {
        // handle errors
    } finally {
        if (parcelFD != null)
            try {
                parcelFD.close();
            } catch (IOException e) {
                // ignored
            }
    }
}

关于java - BitmapFactory 无法解码流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21195899/

相关文章:

java - JSF 和/或 SEAM 中是否有内置的 Y/N 和 boolean 值转换器可与 h :selectBooleanCheckbox? 一起使用

java - src/main/resources 中的 Maven Jar 主类

java - 是否存在评估 java 项目的 2 个不同 svn 版本之间的 "programming effort"的指标?

android - 如何在 Xamarin 中打开默认浏览器?

android - 在 Android 浏览器中仅使用 HTML 和 CSS 的上拉工具栏

python - 将子颜色名称转换为父颜色名称

java - 检查下一个数组元素是否与Java中的当前元素不同

android - 如何通过Android中的YouTubeDataApi v3获取YouTube视频的时长?

php - Polymer,使用ajax形式发送图片

JavaScript Image().width 返回 0