My application contains lot of images loading at runtime. This app works fine in almost all android devices but in Xperia it oftenly crashes with low virtual memory.
plz help me...
you need to catch the low memroy error and reduces the size of downloaded image as below.
catch(OutOfMemoryError e)
{
e.printStackTrace();
Log.e("Out of memory error", e.toString());
reduce_size();
}
.....
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
options.inJustDecodeBounds = true;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
final int REQUIRED_SIZE=70;
int width_tmp=options.outWidth, height_tmp=options.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap btm=BitmapFactory.decodeStream(is, null, o2);
img_t.setImageBitmap(btm);
Related
I'm using Images only of the resolution xxhdpi to reduce the size of apk
If I run my app in xxhdpi device, its fine.
When I run my app in hdpi device it is responding very slowly.
Is this time being taken to render xxhdpi image in hdpi ??
Is this a correct procedure to use ??
No that's not correct procedure, it is slow, sometimes it may also kill your app saying MemoryOutOfException etc...
You've to programmatically decode and downgrade the image using BitmapFactory
For example
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(!(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)){
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
I'm writing an app that integrates with Facebook.
My app has an option to show a friends' list.
Each item in the list has a ProfilePictureView:
<com.facebook.widget.ProfilePictureView
android:id="#+id/fli_profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:is_cropped="true"
app:preset_size="small" />
And then the list adapter sets the users' ids into each item's ProfilePictureView :
ProfilePictureView profilePicture = (ProfilePictureView) view.findViewById(R.id.fli_profilePicture);
profilePicture.setProfileId(friendsList.get(position).getId());
But this way, if you scroll too much, the ListView throws an OutOfMemoryError.
Is it because the ProfilePictureView downloads large versions of the profile pictures, and then only scales them to small? If so, how do I set it to just download the pictures in small sizes?
Or is there another way to combat this?
Thanks
You should probably reduce memory consumption by scaling down downloaded images
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
here a nice tutorial to download images in ListView
http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/
Hope that helps you :)
I have compressed the image using this:
private Bitmap decodeFile(InputStream is){
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=100;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(is, null, o2);
}
I am setting this bitmap in an image view but it leaves some space on top and bottom.
I tried this:
img.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT));
img.setImageBitmap(bm);
but of no use.
Is there any other way?
This may solve the issue.
iv.setScaleType(ScaleType.FIT_XY);
Use this, in your activity,
mImageView.setScaleType(ScaleType.FIT_XY);
or in your layout,
android:scaleType="fitXY"
I am using https://github.com/thest1/LazyList for image caching. I have to diaplay image in full screen. but there are big loss in quality of image.
which code i have to change for get original image.thanks in advance.
Look for this method in the ImageLoader class,
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
And remove the below lines from this method,
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
This will make sure that your Image doesn't get scaled at all.
But you have to keep in mind that, your app is vulnerable to OOM if you do this.
In your ImageLoader.java the function
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f)
is used to scale / resize ur image.
final int REQUIRED_SIZE=70;
increase this to increase the quality of the image. Make it 200 or something and try.
I am getting different size images from server and setting in list-view but while maintaining resolution of images some get blur. I am loading a images using lazy-list concept.
Approximate size of my images.
width height
783 435
108 68
320 179
72 54
596 335
Below Method is used to handle the images resolution.
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=50;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=2;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
And if I remove or changes to o2.inSampleSize=1 then it wont work in Galaxy SII.
Thanks.