【问题标题】:How to get bitmap from file manager?如何从文件管理器中获取位图?
【发布时间】:2016-03-30 15:19:13
【问题描述】:

我将在我的 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 以压缩此图像。

第二次,options 不等于 null - 我通过调试检查它。

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

【问题讨论】:

    标签: android android-intent bitmap bitmapfactory


    【解决方案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;
    

    【讨论】:

    • 将流位置设置回开始可能会修复它。或者获得一个新的流。因为原来的那个已经被消耗掉了。
    • 是的,我会尝试保存原始流,当我第二次解码时,将使用原始的副本。但是,我需要使用它两次来设置选项,因为,我还需要裁剪图像并减轻重量(例如,不是 2 mb,200 kb)。
    • 是的,我得到了两次 InputStream,它对我有帮助!非常感谢!
    【解决方案2】:

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

    【讨论】:

    • 您能否在您的答案中添加更多信息 - 至少是 inputstreamoptions 的初始变量,并确保解释为什么您使用 null 作为参数!谢谢!
    【解决方案3】:

    从文档中,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.

    如果选项参数为空(或表现为空),则结果将为空:

    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)
    

    【讨论】:

      【解决方案4】:

      如果你尝试这种方式会怎样:

      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);
          }
      }
      

      【讨论】:

      • 不幸的是,它对我没有帮助,我也不知道为什么:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多