Is there anyway to resize/scale bitmap without creating a new bitmap? Let say i download image that height or width is larger than 2048px. Before i can display it, i have to resize it because ImageView does not support bitmaps larger than 2048px. If i use Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) it gives me a new bitmap. Now we have two bitmaps, original and scaled. Thats when my application runs out of memory.
Below is the code which i use right now.
// Async task to download the image
private ImageView mImage;
private ProgressBar progress;
private Button button;
@Override
protected void onPostExecute(Bitmap result){
progress.setVisibility(View.GONE);
if(result != null){
if(result.getHeight() > 2048 || result.getWidth() > 2048){
float scaledvalues[] = scale(result.getWidth(), result.getHeight());
image = Bitmap.createScaledBitmap(result, (int)scaledvalues[0], (int)scaledvalues[1], false);
mImage.setBitmap(image);
}
else{
image = result;
mImage.setBitmap(result);
}
button.setEnabled(true);
}
}
//I use this method to calculate new width and height
public float[] scale(int width, int height){
float scaledheight = -1f;
float scaledwidth = -1f;
float scaledheightpros = -1f;
float scaledwidthpros = -1f;
float finalheight = -1f;
float finalwidth = -1f;
if(height > 2048){
scaledheight = height - 2048f;
float s = scaledheight*100f;
scaledheightpros = s / 100f;
}
if(width > 2048){
scaledwidth = width - 2048f;
float z = scaledwidth * 100f;
scaledwidthpros = z / width;
}
if(scaledheightpros > scaledwidthpros){
float a = height/100f;
float b = width/100f;
finalheight = height - (a * scaledheightpros);
finalwidth = width - (b * scaledheightpros);
}
else{
float a = height/100f;
float b = width/100f;
finalheight = height - (a * scaledwidthpros);
finalwidth = width - (b * scaledwidthpros);
}
Log.i(TAG, "startingheight: " + height + " finalheight: " + finalheight + "%: " + scaledheightpros);
Log.i(TAG, "startingwidth: " + width + " finalwidth: " + finalwidth + "%: " + scaledwidthpros);
float array[] = {finalwidth, finalheight};
return array;
}