mercredi 25 mars 2015

OutOfMemoryError android even after decoding


Vote count:

-1




I'm loading image from SharedPreferences. It works fine, but sometimes goes into "OutOfMemoryError" exception. After searching, I also added solution provided by Google android developer site ([Here]) But still I'm getting same error. Below is my code- 1



Uri storedUri = Uri.parse(savedCarImg);
try {
InputStream imageStream = getContentResolver().openInputStream(storedUri);
// Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);

ImageView carImg = (ImageView) findViewById(R.id.userCarImg);


final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 100, 100);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;

Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream,null,options);
carImg.setImageBitmap(yourSelectedImage);
}
catch(FileNotFoundException e){

}


and the method



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;
}
}
return inSampleSize;
}


Please help me in fixing the problem, whats going wrong. Thank you in advance.



asked 2 mins ago







OutOfMemoryError android even after decoding

Aucun commentaire:

Enregistrer un commentaire