3

我需要调整/缩小从画廊中选择的图像,然后再从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;
        }
    }
4

1 回答 1

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);
于 2014-09-07T19:23:24.700 回答