如何从文件管理器获取位图?

时间:2016-03-30 15:19:14

标签: android android-intent bitmap bitmapfactory

我要在我的Android应用程序中创建附件。我需要附上图片。 这是我的代码:

...
attachButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "Select image"), CHOOSE_IMAGE);
        }
    });
...


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CHOOSE_IMAGE) {
        if(resultCode == RESULT_OK) {
            Uri uri = data.getData();
            ImageView imageView = new ImageView(this);

            Bitmap bitmap = getDecodedImageFromUri(uri);
            imageView.setImageBitmap(bitmap);
         }
    }
 }

 private Bitmap getDecodedImageFromUri(Uri uri) {
    InputStream inputStream = null;
    try {
        inputStream = getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Rect rect = new Rect(0, 0, 0, 0);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;

    BitmapFactory.decodeStream(inputStream, rect, options);
    options.inSampleSize = getInSampleSize(options, 128, 128);

    options.inJustDecodeBounds = false;
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream, rect, options); //HERE IS PROBLEM - bitmap = null.
    return bitmap;
}

private int getInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int width = options.outWidth;
    int height = options.outHeight;
    int inSampleSize = 1;

    if(height > reqHeight || width > reqWidth) {
        int halfHeight = height / 2;
        int halfWidth = width / 2;

        while ((halfHeight / inSampleSize) > reqHeight &&
                (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

我添加评论哪里有问题。 所以,我做了调试,在这一刻:

 Bitmap bitmap = BitmapFactory.decodeStream(inputStream, rect, options);

位图等于null。

有什么问题?我做错了什么?

如您所见,这些辅助方法来自android开发人员指南。

已更新

我需要解码两次,因为我需要获取选项,然后获取图像大小来计算InSampleSize以压缩此图像。

第二次,选项不等于null - 我通过debug检查它。

但是,在第二个解码选项的outWidth和outHeight为-1之后。所以,它设置为默认值。我不知道这一刻发生了什么。

4 个答案:

答案 0 :(得分:1)

我猜您的问题可能是因为调用 decodeStream 两次

BitmapFactory.decodeStream(inputStream, rect, options); //HERE
options.inSampleSize = getInSampleSize(options, 128, 128);

options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, rect, options); //HERE AGAIN
return bitmap;

答案 1 :(得分:0)

您可以尝试BitmapFactory.decodeStream(inputStream, null, options);

答案 2 :(得分:0)

从文档中,BitmapFactory.Options的默认构造函数可能会给你这个麻烦:

  

BitmapFactory.Options() Create a default Options object, which if left unchanged will give the same result from the decoder as if null were passed.

如果options paramter为null(或表现为null),那么结果将为null:

  

The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)

尝试制作更好的选项对象或使用方法

public static Bitmap decodeStream (InputStream is)

答案 3 :(得分:0)

如果你这样尝试怎么办:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        Uri imageUri = data.getData();
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    }
}