Android: OutOfMemory while calling BitmapFactory.decodeStream() - java

I'm developing an Android app that has to display some pictures (usually taken by the phone's camera) on the screen. The way that I've created a bitmap is:
Uri uri = Uri.withAppendedPath(Images.Media.EXTERNAL_CONTENT_URI, "" + mediaId);
bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(uri));
The 2nd line of this ends up on some handsets with OutOfMemoryException. Am I doing something wrong or maybe is there any alternative way of doing the same?
Thanks in advance for any ideas

The photos from the camera are several MBs large. Android applications have a maximum of 24MB RAM available (see What is the maximum memory limits per application for Android 2.2?)
So if your application has several images from the camera, it won't fit in the RAM available to your application.
Scaling down your image(s) is the only solution I am afraid...

Related

Slow processing of all images by google face API?

I am making an android app which is using the Google face API to detect faces of all the images in the gallery. It is taking a long time to process all the images and hence the apps get stuck for a long time. Any workaround?
I tried reducing the size of the image and then process, but it gives a faulty answer on it.
If you look in the documentation of the FaceDetector.Builder you will see that you can set some properties that will increase the speed.
E.g.:
public FaceDetector.Builder setProminentFaceOnly (boolean prominentFaceOnly)
Disable the image tracking :
FaceDetector detector = new FaceDetector.Builder(context)
.setTrackingEnabled(false)
.build();
It's true by default, and may slow the detection if you don't need this feature.
2 minutes for 715 images is a really good time.
Steps that can be taken:
enable fast mode in FaceDetector
set setTrackingEnabled to false if you don't want to track
set minimum face size to an appropriate size according to your dataset
Load the bitmaps using Universal Image Loader or glide library of Android. I used the UIL library.
640x480 is an optimum size for face detection and classification for scale down the size for less time and almost the same result.
Set setLandmarkType and setClassificationType according to your needs and disable if not required.

Android:VM crashed for background drawable

I make a style which assigns the background for the application and add the style in the mainfest.xml.
I made two pictures in drawable-land and drawable-port for the application's background drawable.When I test my app on the phone and rotate the phone some times, the app crashed for the app background drawable overflow.
SO how can I control this? Is there anyone can tell me how to fix it? Thanks in advance!
if the exception/error that is shown in the logcat is outOfMemory (something like "java.lang.OutofMemoryError: bitmap size exceeds VM budget." ) , you need to handle the image in a more efficient way since it takes too much memory for the device/emulator to use.
i would recommend to either lower the quality , depending on the density of the device , and also read the android bitmap handling tips

Out of memory error with loading image

I want to load an image on android
background = BitmapFactory.decodeResource(getResources(),R.drawable.hangmanbegin);
background = Bitmap.createScaledBitmap(background,screenx,screeny,false);
The image is 800*1280 pixels , so if I'm correct it should use arround 3MB of memory space?
But my heap grows from 15MB to 29MB just at that phase , so no window or context leaking?
How is this explained? en what can you do about it?
Thnx in advance!
Bitmaps take up a lot of memory, especially for rich images like
photographs. For example, the camera on the Galaxy Nexus takes photos
up to 2592x1936 pixels (5 megapixels). If the bitmap configuration
used is ARGB_8888 (the default from the Android 2.3 onward) then
loading this image into memory takes about 19MB of memory (2592*1936*4
bytes), immediately exhausting the per-app limit on some devices.
from http://developer.android.com/training/displaying-bitmaps/index.html
credit and below it a way to approach a fix https://stackoverflow.com/a/10127787/643500

Bitmap size exceeds VM budget when game development

I am developing a game on android.Like tower defense.
I am using surface view.I am using some image as bitmap.(Spritesheets, tilesets, buttons, backgrounds,efects vs.)
Now images are nearly 5-6 mb.And i get this error when i run my game:
Bitmap size exceeds VM budget
19464192-byte external allocation too large for this process.
I call images like that
BitmapFactory.decodeResource(res, id)
and i put it to array.
I can't scale images.I am using all of them.
I tried that
options.inPurgeable=true;
and it work but the image is loading very slowly.I load a spritesheet with that and when it is loading, i get very very low fps.
What can I do?
I've had this problem too; there's really no solution other than to reduce the number/size of bitmaps that you have loaded at once. Some older Android devices only allocate 16MB to the heap for your whole application, and bitmaps are stored in memory uncompressed once you load them, so it's not hard to exceed 16MB with large backgrounds, etc. (An 854x480, 32-bit bitmap is about 1.6MB uncompressed.)
In my game I was able to get around it by only loading bitmaps that I was going to use in the current level (e.g. I have a single Bitmap object for the background that gets reloaded from resources each time it changes, rather than maintaining multiple Bitmaps in memory. I just maintain an int that tracks which resource I have loaded currently.)
Your sprite sheet is huge, so I think you're right that you'll need to reduce the size of your animations. Alternatively, loading from resources is decently fast, so you might be able to get away with doing something like only loading the animation strip for the character's current direction, and have him pause slightly when he turns while you replace it with the new animation strip. That might get complicated though.
Also, I highly recommend testing your app on the emulator with a VM heap set to 16mb, to make sure you've fixed the problem for all devices. (The emulator usually defaults to 24mb, so it's easy for that to go untested and generate some 1-star reviews after release.)
I am not a game dev however I would like to think I know Android enough.
Loading images of the size is almost certain to throw errors. Why are the images that file size?
There is an example at http://p-xr.com/android-tutorial-how-to-paint-animate-loop-and-remove-a-sprite/. If you notice he has an explosion sprite of only ~200Kb. Even a more detailed image would not take much more file space.
OK some suggestions:
Are you loading all your spritesheets onto a single sheet or is
each spritesheet in a seperate file? If they are all on one I would
split them up.
Lower the resolution of the images, an Android device is portable
and some only have a low resolution screen. For example the HTC
Wildfire has a resolution of 240x320 (LDPI device) and is quite a
common device. You have not stated the image dimensions so we can't be sure if this is practical.
Finally; I am not a game programmer but I found this tutorial (part of the same series) quite enlightening - http://p-xr.com/android-tutorial-2d-canvas-graphics/. I wonder if you are applying a pattern that is not appropriate for Android, however without code I cannot say.
Right something a little off topic but worth noting...
People under estimate the power of the View. While there is a certain amount of logic to using a SurfaceView, the standard View will do quite a lot on its own. A SurfaceView more often than not requires an underlying thread to run (that you will have to setup yourself) in order to make it work. A View however calls onDraw(), which can be utilized in a variety of ways including the postinvalidate() method (see What does postInvalidate() do?).
In any case it might be worth checking out this tutorial http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/. Personally, it was an excellent example of a custom View and what you can do with them. I rewrote a few sections and made a pocket watch app.

Full Resolution Camera Access in j2me

I'm trying to do an image capture on a high end Nokia phone (N95). The phone's internal camera is very good (4 megapixels) but in j2me I only seem to be able to get a maximum of 1360x1020 image out. I drew largely from this example http://developers.sun.com/mobility/midp/articles/picture/
What I did was start with 640x480 and increase the width and height by 80 and 60, respectively until it failed. The line of code is:
jpg = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=" + width + "&height=" + height);
So the two issues are:
1. The phone throws an exception when getting an image larger than 1360x1020.
2. The higher resolution images appear to be just smoothed versions of the smaller ones. E.g. When I take a 640x480 image and increase it in photoshop I can't tell the difference between this and one that's supposedly 1360x1020.
Is this a limitation of j2me on the phone? If so does anyone know of a way to get a higher resolution from within a j2me application and/or how to access the native camera from within another application?
This explanation on Nokia forum may help you.
It says that "The maximum image size that can be captured depends on selected image format, encoding options and free heap memory available."
and
"It is thus strongly adviced that at least larger images (larger than 1mpix) are captured as JPEG images and in a common image size (e.g. 1600x1200 for 2mpix an so on). Supported common image sizes are dependent on product and platform version."
So I suggest you to take some tries
1. with 1600x1200, 1024x768 and whatever image resolution your N95 guide mentions
2. with BMP and PNG as well.
Anyway, based on my earlier experiences (that could be outdated), j2me implementations are full of bugs, so there may not be a working solution to your problem.
Your cameras resolution is natively:
2582 x 1944 . Try capturing there to see how that goes.
This place:
http://developers.sun.com/mobility/midp/articles/picture/index.html
Mentions the use of:
byte[] raw = mVideoControl.getSnapshot(null);
Image image = Image.createImage(raw, 0, raw.length);
The use of raw seems interesting, to get the native resolution.
The 'quality' of a JPEG (As interpreted by the code) is nothing to do with the resolution. Rather it is to do with how compressed the image is. A 640x480 image at 100 quality will be noticably better looking than a 640x480 image at 50, but will use more storage space.
Try this instead:
jpg = mVideoControl.getSnapshot("encoding=jpeg&quality=100&width=2048&height=1536");

Categories