JPEG File seems to big: OutOfMemoryError - android

java.lang.OutOfMemoryError: Failed to allocate a 313194252 byte allocation with 11901284 free bytes and 174MB until OOM
I got this at this line:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.handskizze);
My Picture has a size of 2480*3508 at 300dpi.
Is it maybe to big? How can I make it smaller?

Whenever there is a requirement to load any image always try to use any ImageLoader library.
There are plenty of options available.
Here are some of the best libraries that developers uses. Try anyone of them which you fill easy to implement.
Universal Image Loader: https://github.com/nostra13/Android-Universal-Image-Loader
Picasso: http://square.github.io/picasso/
Glide: https://github.com/bumptech/glide
You can find some more libraries but I feel these 3 are used most.

In manifest use below attribute with application tag
android:largeHeap="true"
Also use
BitmapFactory.Options o=new BitmapFactory.Options();
o.inSampleSize = 2; //change value from 0 to 8 as per need and image resolution
myBitMap=BitmapFactory.decodeResource(getResources(),R.drawable.handskizze, o);

First of all, i would say your image is way too big for mobile devices. You should scale it down using Photoshop (or any other image editing software) to reduce its size. By doing so you minimize the size of your .apk and the processing time it takes to downscale the image.
Additionally, i would recomment you to use an image loader library. Im using the Universal Image Loader in most of my projects. It gets the measurement of the ImageView you want to load your image into, and downscales the image to reduce its size. You should give it a try ;)

Related

How to increase Image Memory using Bitmap Factory option

I want to use Big Size Image In Back Ground so How Can I do And i want to It's Working For All Device
As per your requirement, I guess that you are getting image from server end. Some time big size Bitmap throw OutOfMemoryException. So do one thing. Use this library to compress image and then show it to ImageView.
https://github.com/zetbaitsu/Compressor
You can also use Library. It provide you facility to fix image size and quality. so it won't be problem for you.
Nostra Image Loader

Fresco. How to get image size without loading full bitmap

I have some local images with unknown size. Format is jpeg.
The task is resize it to fit max size 1280x960 keeping original aspect ratio and save it to new file with low memory consumption. I found out very useful method in Fresco lib: JpegTranscoder.transcodeJpeg() operating streams. It has scaleNumerator param that rules downsampling. How can I get original bitmap size with Fresco without full bitmap loading to memory to calculate scaleNumerator value? Something like inJustDecodeBounds in android BitmapFactory.Options class
You should be able to use Fresco's BitmapUtil.decodeDimensions(...) for this.

Picasso's image loading using fit() method

If I am using Picasso's fit() method in loading images from online, do I have to consider using BitmapFactory.Options.inSampleSize options to reduce the size of an image? I read from other stackoverflow's question (Here) that:
You can also make use of the BitmapFactory.Options.inSampleSize
options to reduce the size of an image while it is loaded, rather than
loading in the entire image and then creating a small copy, which
wastes time and memory.
Do I have to do it after using fit()? Or it is already done by Picasso?
Thanks a lot.
Both fit() and explicit calls to resize() (or resizeDimen) will use inSampleSize automatically when it is appropriate to do so.
Because inSampleSize should be a power of two for best performance and quality, your image needs to be at least twice as large as the target size for this to occur.

Faster image processing

Can someone suggest me a library that can do simplest operations like scale, crop, rotate without loading image fully into memory?
The situation: I need to scale image down from a very large size, but the scaled down image is still too large to be allocated in memory (if we use standard android tools). Since I only need to upload scaled down version, I thought of scaling it through native library and upload it through FileInputStream.
I've tried to use ImageMagic and it does the job, but performance is very poor (maybe there is a way to speed things up?)
Might want to check out OpenCV for Android
You can use the original Android Bitmap functionality by pulling the image into memory but allowing Android to sample the image before it is loaded.
For example:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap myBitmap = BitmapFactory.decodeStream(inputstream,null,options);
This will load your bitmap into memory with half the memory footprint of the full image. You can experiment with changing the inSampleSize to get a good fit for your application.
You can also calculate the sample size on the fly, if you know the final image size you are aiming for, you can get the current file size of the image before you load it into memory and calculate the sample size using the equation inSampleSize = OriginalSize/RequiredSize. Though sample size is best used when it is a power of 2, so you can make adjustments for this.
Edit:
A great example here https://stackoverflow.com/a/823966/637545

Large source ImageView animation

What's the best image file format for Android in terms of memory? PNG is recommended for iOS as xCode does some magic with it.. Is it the same for Android?
I'm currently developing a big app with multiple animations going on (sliding in screens, fading etc etc). All works well so far! However I have noticed the view animation where the view contains an ImageView with a (quite large) PNG as the source is a bit laggy.
Obviously I can make the PNG smaller, but is there anything extra I can do to reduce the amount of memory the ImageView takes up/makes the animation smooth? I know PNG has a much larger file size than JPEG, but I can't see this being a problem, the JPEG or PNG (I assume) is eventually stored as an array of colours, so they would both take up the same memory. PNG is probably better for loading due to less cycles uncompressing. Again I only assume, my knowledge of image file formats is null.
Alternatively is there anything else causing the lag? Is the bitmap scaled to fit the view each onDraw() during the animation so should I scale the bitmap in code before giving it to the ImageView?
Thanks,
The formats supported by Android are: PNG, JPG and GIF (also 9.png).
The recomendated is PNG as said in dev guide
All of them are stored in memory as a Bitmap, so the most important thing is the color deph, like this:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon, options);
More info: stackoverflow
and add (after honeycomb):
<application
android:largeHeap="true"
...
to your manifest file :=)
thanks to my dear friend :)

Categories

Resources