【问题标题】:Android - Resize image before BitmapFactory.decodeStream returns itAndroid - 在 BitmapFactory.decodeStream 返回之前调整图像大小
【发布时间】:2014-11-01 01:22:16
【问题描述】:

在从BitmapFactory.decodeStream(imagestream) 返回之前,我需要调整从图库中选择的图像的大小/缩小它。当我选择大图像时,它会抛出OutOfMemory Exception。在从 decodeStream 返回之前如何调整图像大小?

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == IMAGE_PICKER_SELECT
                && resultCode == Activity.RESULT_OK) {
            Uri selectedImage = data.getData();
            InputStream imageStream = null;
            try {
                imageStream = getContentResolver().openInputStream(
                        selectedImage);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Bitmap bitmap = BitmapFactory.decodeStream(imageStream);
            imgV.setImageBitmap(bitmap);
            // sb = bitmap;
            // imgSelected = true;
            selectedImage = null;
            imageStream = null;
            bitmap = null;
        }
    }

【问题讨论】:

  • 参考this SO question
  • @Sash_KP :您提供的链接处理位图,但 OP 在解码位图时遇到问题,这就是他想要调整大小的原因。 How could I resize the image before it is returned from decodeStream他想使用imagestream调整大小

标签: android android-bitmap shrink


【解决方案1】:

试试这个方法

BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=2; //decrease decoded image 
Bitmap bitmap=BitmapFactory.decodeStream(is, null, options); 
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);

【讨论】:

  • .compress 中的 fos 是什么,有必要吗?
  • @user3245899 fos 是一个文件输出流
猜你喜欢
  • 2011-04-16
  • 2012-12-14
  • 2011-09-01
  • 2013-07-26
  • 2015-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多