How display the image of bitmap in ImageView using BitmapFactory.Options? - android

i follow the steps in developers.android.com in best practice for Loading Large Bitmaps Efficiently, but i cant let the video thumbnail image apear in the imageview, what is the problem?
holder.imageHolder.setImageBitmap(decodeSampledBitmapFromResource(
aListData.get(position).path, 130, 100));
public Bitmap decodeSampledBitmapFromResource(String path,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
public 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;
}
If it came from the developers.android.com it should work but i follow the guide but it didn't work.
reference: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Related

OutOfMemoryError when adding imageView programmatically

I want to add a few ImageView in a linearLayout programmatically.
but application crashed for more than 2 images. its error is OutOfMemoryError.
String[] titleImages={"a","b","c","d","e","f","g"};
for (String title : titleImages){
InputStream inputStream = context.getAssets()
.open("titles"+"/"+title);
Drawable drawable = Drawable.createFromStream(inputStream, null);
inputStream.close();
ImageView imageView = new ImageView(_context);
imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageDrawable(drawable);
//more imageView set properties
LinearLayout shelf=findViewById(R.id.shelf);
//shelf is a LinearLayout
shelf.addView(imageView);
}
Use smaller images.
Or, use BitmapFactory.Options, an in particular its inSampleSize option, to downsample the images to something that is more appropriate for your screen size.
Or, use a third-party image-loading library, like Picasso, that can handle inSampleSize for you.
by using these methods you can resize the image as per your use.
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);
}
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;
}

Load High quality image to imageview

I want to load a 15Mb image file into an imageview.. I tried loading it using piccasa and some other ways, But all return out of memory exception.
The code I used is
Picasso.with(getActivity()).load(R.drawable.highqual).into(imageView);
Is there any way to make it possible in android
try this:
call this function decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight)
which reqHeight and reqWidth is your ImageView dimension. also note that if it takes long time and send you ANR run it on a separate thread then set it to your ImageView.
the code is from doc that I changed it to read image file.
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;
}
public static Bitmap decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
Reference:
Loading Large Bitmaps Efficiently

OutOfMemoryError android Bitmap listview

In my application I use an SQLite database to store the data of a lot of images. Some images can be up to 600x600 pixels. I use a custom list to create the bitmaps. I know there is a method bitmap.recycle(); but I'm not sure how to use that with a listview.
Here is the solution to handle bitmaps in android
First, you should calculate the sampleBitmapSize of the bitmap ( to load the lower version of the same bitmap )
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);
}
Below is the util function to calculate InSampleSize ( an integer that defines the value (rating) of quality of a bitmap).
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;
}
}
Log.i("ImageUtil", "InSampleSize: "+inSampleSize);
return inSampleSize;
}

Android: failed to open 3 times a very large image

I am trying to load a very large image in my app (2000x3000 and 600kb). I manage to make it open with this code:
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);
}
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;
}
But I get an OutOfMemoryError when I open it 3 times or try to open another activity. How I can fix it?
thanks
"But I get an OutOfMemoryError when I open it 3 times or try to open
another activity. How I can fix it?"
Release your resources! Call bitmap.recycle() and null the bitmap.

Get memory leak when decode small jpg image in Android

I decode a small jpg image to bitmap in my app.
The image is only 40K.
I use these methods:
public Bitmap decodeSampledBitmapFromResource(String pathName,int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathName, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp=BitmapFactory.decodeFile(pathName, options);
return bmp;
}
public 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;
}
and when the program goes to:
Bitmap bmp=BitmapFactory.decodeFile(pathName, options);
I get GC_Concurrent in the LogCat,
why?
GC_CONCURRENT per se is not bad. It's just a status message from the garbage collector doing its job. Image decoding involves memory allocations and it's normal to see garbage collector running.

Categories

Resources