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

时间:2014-09-07 19:13:48

标签: 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;
        }
    }

1 个答案:

答案 0 :(得分:3)

以这种方式试试

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