Skip to main content

how How to avoid an out of memory error while using bitmapbitmaps in androidAndroid

iI am using bitmapbitmaps. When the code runs it shows an out of memory error. hotHow can itthe error be avoided. myMy code is followingfollows. Thanks in advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap); public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 

how to avoid out of memory error while using bitmap in android

i am using bitmap. it shows out of memory error. hot can it be avoided. my code is following. advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap); public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 

How to avoid an out of memory error while using bitmaps in Android

I am using bitmaps. When the code runs it shows an out of memory error. How can the error be avoided. My code follows. Thanks in advance.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap); public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null; final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 
added 20 characters in body
Source Link
Tarsem Singh
  • 14.2k
  • 7
  • 53
  • 71

i am using bitmap. it shows out of memory error. hot can it be avoided. my code is following. advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap);

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null;

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap); public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null;  final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 

i am using bitmap. it shows out of memory error. hot can it be avoided. my code is following. advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap);

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null;

 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 

i am using bitmap. it shows out of memory error. hot can it be avoided. my code is following. advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap); public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null;  final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } } 
Source Link

how to avoid out of memory error while using bitmap in android

i am using bitmap. it shows out of memory error. hot can it be avoided. my code is following. advance to thanks.

Bitmap myBitmap = Image.decodeSampledBitmapFromUri(path, 250, 500); img_cook[index].setImageBitmap(myBitmap);

public static Bitmap decodeSampledBitmapFromUr(String path, int reqWidth, int reqHeight) { Bitmap bm = null;

 final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; bm = BitmapFactory.decodeFile(path, options); return bm; } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float)height / (float)reqHeight); } else { inSampleSize = Math.round((float)width / (float)reqWidth); } }