Android - 在 BitmapFactory.decodeStream 返回之前调整图像大小

标签 android android-bitmap shrink

在从 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;
        }
    }

最佳答案

这样试试

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

关于Android - 在 BitmapFactory.decodeStream 返回之前调整图像大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25713829/

相关文章:

android - keyOutputText 在软键盘上不起作用

java - 将列表传递给自定义 View ?

azure - 如何缩小 Azure SQL Server DB(5GB 服务器空间已收取 18MB 数据费用)

python - pyGTK 自动调整笔记本选项卡大小

ios - 从左到右收缩 UIView

java - 在android中有时无法生成随机图像

android - 类路径中的运行时 JAR 文件的版本为 1.4,比 API 版本 1.5 旧?

android - 当在 xml 布局中声明 2 个 ImageView 时,内存中有 2 个可绘制副本?

Android:将 Canvas 绘制到 View

android - 在 Canvas 上绘制多条线,中间有延迟