I am new to android studio, I am trying to make an app that contains large number of images and of course I am getting the "OutOfMemory" error . I have searched for the solution and I found here a suggested method to solve it at this topic https://developer.android.com/topic/performance/graphics/load-bitmap.html
I have done the same method but after calling the decodeSampledBitmapFromResource, the image doesn't appear
here is the part of my code to call it
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.step2pointaprob4exc2img);
image.setImageBitmap(ImageNiver.decodeSampledBitmapFromResource(getResources(),R.id.step2pointaprob4exc2img,reqwidth1,reqheight1));
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int reqwidth1 = size.x;
int reqheight1 = size.y;
String s = String.valueOf(reqwidth1);
String ss = String.valueOf(reqheight1);
Log.i("value of s :",s);
Log.i("value of ss :",ss);
and here is the method i used at the class imageNiver
public class ImageNiver {
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
// This offers some additional logic in case the image has a strange
// aspect ratio. For example, a panorama may have a much larger
// width than height. In these cases the total pixels might still
// end up being too large to fit comfortably in memory, so we should
// be more aggressive with sample down the image (=larger inSampleSize).
long totalPixels = width * height / inSampleSize;
// Anything more than 2x the requested pixels we'll sample down further
final long totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels > totalReqPixelsCap) {
inSampleSize *= 2;
totalPixels /= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
}
I have been searching for the solution for days but i couldn't find the answer to this problem.
In this line:
image.setImageBitmap(ImageNiver.decodeSampledBitmapFromResource(getResources(),R.id.step2pointaprob4exc2img,reqwidth1,reqheight1));
look twice at this R.id.step2pointaprob4exc2img
you should pass the id of the image in your resources (actual picture), not the id of the imageView like you are doing here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.