The Bitmap image is too large, causing memory overflow. Generally, the image preview mechanism is used to obtain an image with a smaller size.
Here, the so-called scale-out does not mean to set the width and height in layout to reduce the image size (in essence, it is still a large memory image), but to actually reduce the image size and reduce memory usage.
The methods are described in the annotations below:
[Java]
Private Bitmap revitionImageSize (String path, int size) throws IOException {
// Obtain the image
InputStream temp = this. getAssets (). open (path );
BitmapFactory. Options options = new BitmapFactory. Options ();
// This parameter indicates that, without allocating memory space for bitmap, only the information of the image (such as the size of the image) is recorded. To put it bluntly, the memory is optimized.
Options. inJustDecodeBounds = true;
// Obtain the options content by creating an image (here the value is assigned by passing the java address)
BitmapFactory. decodeStream (temp, null, options );
// Close the stream
Temp. close ();
// Generate a compressed image
Int I = 0;
Bitmap bitmap = null;
While (true ){
// This step satisfies the width and height according to the size to be set.
If (options. outWidth> I <= size)
& (Options. outHeight> I <= size )){
// Re-obtain the stream. Note: The stream must be loaded again. You cannot use the previous stream again!
Temp = this. getAssets (). open (path );
// This parameter indicates that the newly generated image is a fraction of the original image.
Options. inSampleSize = (int) Math. pow (2.0D, I );
// If this parameter is set to true, set it to false. Otherwise, no image is created.
Options. inJustDecodeBounds = false; www.2cto.com
Bitmap = BitmapFactory. decodeStream (temp, null, options );
Break;
}
I + = 1;
}
Return bitmap;
:
Author: xyz_fly