Resize a large bitmap file to scaled output file on Android
No. I'd love for someone to correct me, but I accepted the load/resize approach you tried as a compromise.
Here are the steps for anyone browsing:
- Calculate the maximum possible
inSampleSize
that still yields an image larger than your target. - Load the image using
BitmapFactory.decodeFile(file, options)
, passing inSampleSize as an option. - Resize to the desired dimensions using
Bitmap.createScaledBitmap()
.
How to Resize a Bitmap in Android?
Change:
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
To:
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
Android how to scale an image with BitmapFactory Options
Use BitmapFactory.Options.inSampleSize when you first load your image to get the size as close as possible to your target size, then use Bitmap.createScaledBitmap
to scale to the exact size you want.
There's some code for this in this answer.
How to resize my bitmap as 16*16 size or 32*32
This may be helpful and easy,
Bitmap bmp=Bitmap.createScaledBitmap(yourbitmap, 32, 32, false);
Resize bitmap inside file without creating extra bitmaps/files?
I'm not clearly understood, what you want to obtain,
but here is a simple code to resize image file
String your_file_path = "image.png";
int set_scale_your_need = 2;
//getting your image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(your_file_path, o);
o.inJustDecodeBounds = false;
o.inSampleSize = set_scale_your_need;
Bitmap bitmap = BitmapFactory.decodeFile(your_file_path, o);
FileOutputStream fos = new FileOutputStream(your_file_path, false);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
How do I scale a streaming bitmap in-place without reading the whole image first?
This method will read the header information from the image to determine its size, then read the image and scale it to the desired size in place without allocating memory for the full original sized image.
It also uses BitmapFactory.Options.inPurgeable, which seems to be a sparsely documented but desirable option to prevent OoM exceptions when using lots of bitmaps. UPDATE: no longer uses inPurgeable, see this note from Romain
It works by using a BufferedInputStream to read the header information for the image before reading the entire image in via the InputStream.
/**
* Read the image from the stream and create a bitmap scaled to the desired
* size. Resulting bitmap will be at least as large as the
* desired minimum specified dimensions and will keep the image proportions
* correct during scaling.
*/
protected Bitmap createScaledBitmapFromStream( InputStream s, int minimumDesiredBitmapWith, int minimumDesiredBitmapHeight ) {
final BufferedInputStream is = new BufferedInputStream(s, 32*1024);
try {
final Options decodeBitmapOptions = new Options();
// For further memory savings, you may want to consider using this option
// decodeBitmapOptions.inPreferredConfig = Config.RGB_565; // Uses 2-bytes instead of default 4 per pixel
if( minimumDesiredBitmapWidth >0 && minimumDesiredBitmapHeight >0 ) {
final Options decodeBoundsOptions = new Options();
decodeBoundsOptions.inJustDecodeBounds = true;
is.mark(32*1024); // 32k is probably overkill, but 8k is insufficient for some jpgs
BitmapFactory.decodeStream(is,null,decodeBoundsOptions);
is.reset();
final int originalWidth = decodeBoundsOptions.outWidth;
final int originalHeight = decodeBoundsOptions.outHeight;
// inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
decodeBitmapOptions.inSampleSize= Math.max(1,Math.min(originalWidth / minimumDesiredBitmapWidth, originalHeight / minimumDesiredBitmapHeight));
}
return BitmapFactory.decodeStream(is,null,decodeBitmapOptions);
} catch( IOException e ) {
throw new RuntimeException(e); // this shouldn't happen
} finally {
try {
is.close();
} catch( IOException ignored ) {}
}
}
Related Topics
Maintain/Save/Restore Scroll Position When Returning to a Listview
Sending and Receiving Sms and Mms in Android (Pre Kit Kat Android 4.4)
Open Another Application from Your Own (Intent)
What Is the Main Purpose of Settag() Gettag() Methods of View
Difference Between "@Id/" and "@+Id/" in Android
Android: Check Whether the Phone Is Dual Sim
Onactivityresult Method Is Deprecated, What Is the Alternative
"Arrayadapter Requires the Resource Id to Be a Textview" Xml Problems
How to Send Emails from My Android Application
How to Start My App When the Phone Starts on Android
Replace Fragment Inside a Viewpager
How to Execute Dex: Multiple Dex Files Define Lcom/Myapp/R$Array;
How to Turn on Front Flash Light Programmatically in Android