I am getting memory error in my android game and i think its caused by my image loading function. everything is working fine on my mobile but on tablet i get memory exceed error.
I am using a matrix because i need to resize the image to a float value. But in this case i have to load the image in full size first and then resize it with the matrix and i think this causes the memory error.
is this the correct way to handle image resize?
public class Sprite {
private Bitmap bitmap = null;
private float scaleX, scaleY;
public Sprite(String path, float targetWidth, float targetHeight, Context context) {
InputStream istr;
AssetManager assetManager = context.getAssets();
Matrix scalematrix = new Matrix();
try {
istr = assetManager.open(path);
bitmap = BitmapFactory.decodeStream(istr);
scaleX = targetWidth / bitmap.getWidth();
scaleY = targetHeight / bitmap.getHeight();
scalematrix.postScale(scaleX, scaleY);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), scalematrix, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
}
}
}
BitmapFactory.Options has public int inSampleSize
-- If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
You may try to use BitmapFactory.decodeXXX(..., BitmapFactory.Options opts)
(decodeFile() etc.)