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;
}
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 using the following code to retrieve an bitmap from image file in android:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
However, the size of the bitmap is more than double the size of file. Eg, for a file of size 520kb, the bitmap size is around 1.3MB. Is there a way I can get the bitmap of the same size as that of file?
The bitmap size is pure memory data without compression. You can calculate the size with 4 bytes per pixel (with your settings).
Your file is probably in a compressed format like jpg. Without a compression it would take up the same space.
The reason why the bitmap is kept uncompressed in memory is basically the performance. You can read the data and work with it much faster in an uncompressed way. For example if you would like to see what color a specific pixel has, you would need to uncompress the data first. That takes time and the more pixels you check the more time it would take.
While reading the file from the storage, the uncompression just takes an ignorable amount of time compared to the complete reading process. So you will not have a big performance impact compared to "on demand" uncompression.
try like this may help you,
public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
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_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
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'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;
}
I get the famous out of memory error. But i have tried many of the suggested solutions on this problem without any luck. I understand that to prevent a bitmap to exceed the memory, you make a static variable of the drawable (Android docs). But that isn't working in my app because i have so many markers as you can see..
Does anyone have a suggestion to a solution?
for(Poi p : poiarray){
WeakReference<Bitmap> bitmap = new WeakReference<Bitmap>(p.get_poiIcon());
if(bitmap!=null){
Drawable marker = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap.get(), 60, 60, true));
annotationOverLays.add(new CustomAnnotation(marker,p,this,mapView));
//mapView.getOverlays().add(new CustomAnnotation(marker,p,this,mapView));
}
}
mapView.getOverlays().addAll(annotationOverLays);
ERROR:
05-23 13:08:31.436: E/dalvikvm-heap(22310): 20736-byte external allocation too large for this process.
05-23 13:08:31.436: E/dalvikvm(22310): Out of memory: Heap Size=23111KB, Allocated=22474KB, Bitmap Size=1505KB
05-23 13:08:31.436: E/GraphicsJNI(22310): VM won't let us allocate 20736 bytes
EDIT:
I think i maybe have localized the problem.. I can trigger that outOfmemory exception if i click on several of the annotations. I use the mapViewBalloons from Here, and when i have open closed 2 several times, my app crashes with the exception. Anyone have similar problem?
There are a lot of ways to solve the out of memory exeption.
Try to set inPurgeable at your BitmapFactory.Options http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html#inPurgeable
or try a PurgeableBitmap.
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/PurgeableBitmap.html
Scale Images to smaller size. If you have a view with an adapter make a list with FIFO and fixed size...
I had the same problem and the following code worked for me:
public static Bitmap decodeFile(File f, boolean goodQuality){
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=100;
//Find the correct scale value. It should be the power of 2.
int scale=1;
if(!goodQuality){
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;
}
i managed to read album artwork from my mp3 file(on sdcard) but the picture is
biger than activity. How can i downsize(compress) picture to 150x150 pixels?
You can use "fitXY" to scale an ImageView:
<ImageView android:layout_width="150dp"
android:layout_height="150dp"
android:scaleType="fitXY" />
You can set the source of the ImageView programmatically.
The following piece of code will do the trick ;)
public static Bitmap decodeFile(File f, boolean goodQuality){
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=100;
//Find the correct scale value. It should be the power of 2.
int scale=1;
if(!goodQuality){
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;
}