Using higher density images for lower resolution devices - android

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

Related

What is the best way to support different screen sizes in Android

I have a bitmap that I want to display on a canvas and I want its size (width and height) to be relative to the screen size, so if I have a big screen the bitmap will be big and if I have a small screen the bitmap will be small).
I have two options:
Create multiple versions of the bitmap, each one in different size, and put them in the appropriate drawable directory.
Use only one bitmap size and just dynamically scale it relatively to the canvas size and draw it as I want.
It seems that Google prefer the first way but I don't know why. Can someone tell me what is the preferred way?
Try this to Decode the Bitmap :
Where imagefilepath is the path name of image,it will be in String covert that to File by using
File photos= new File(imageFilePath);
Where photo is the File name of the Image,Now you set your height and width according t your requirements.
Bitmap bitmap = decodeFile(photo);
bitmap = Bitmap.createScaledBitmap(bitmap,150, 150, true);
imageView.setImageBitmap(bitmap);
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++;
}
//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;
}
If you need to know only the best out of the two options, then I'd say option 1. Option 2 will be trouble for small phones which cannot handle large bitmaps.

android-getting error out of memory with showing images

I've an imageview that shows an image everytime user run the app . the images are in drawable folder .
every so often the app crash and in the lof cat it says java.lang.outOfMemmoryError
I've lots of images, about 100 images. 100,000 Byet .
What is the solution to show these images to don't use lot of memory ?
thanks so much
To fix OutOfMemory you should do something like that:
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options);
This inSampleSize option reduces memory consumption.
Here's a complete method. First it reads image size without decoding the content itself. Then it finds the best inSampleSize value, it should be a power of 2. And finally the image is decoded.
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);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
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;
}

(Android)(Facebook) Multiple ProfilePictureViews cause OutOfMemoryError

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 :)

Resizing images in android and iOS

We have an application, running on iOS and on Android. There is a function in app, that loads images to server with specific size ( < 2 mb) So before loading images to the server I need to resize it to reduce image size to successfuly load it to the server.
Is there any solution to make photos compatible with the most part of screen sizes (I need to show full size of image on the screen), running on Android and iOs (no iPad)?
thanks
try this code
private Bitmap decodeBitmapFile(File f,int size,int scal){
try {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
final int REQUIRED_SIZE=size;
int scale=scal;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}

Handle resolution of different size images in listview

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.

Categories

Resources