Android:VM crashed for background drawable - java

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

Related

Real time logo detection in live camera preview with OpenCV on Android

I am new to computer vision but I am trying to code an android app which does the following:
Get the live camera preview and try to detect one logo in that (i have the logo in my resources). In real-time. Draw a rect around the logo if found. If there is no match, dont draw the rectangle.
I already tried a couple of things including template-matching and feature detection using ORB.
Why that didnt work:
Template-matching:
Issues with scaling and rotation. I tried a multi scale variant of it but a) the performance was really bad and b) the rectangle was of course always shown trying to search for the image. There was no way to actually confirm in the code if the logo was found or not.
ORB feature detection:
Also pretty slow (5-6 fps) but it worked ok-ish. The other problem was that also i never could be sure if the logo was in the picture or not. ORB found random matches even if the logo was not in the picture.
Like I said, I am very new to this. I would appreciate the help on what would be the best way to achieve:
Confirm if a picture A (around 200x200 pixels) is in ROI of camera picture (around 600x600 pixels).
This shouldnt take longer than 50ms per frame. I dont know if thats even possible though. So if a correct way to do this would take a bit longer than that, I would just do the work in a seperate thread and only analyze like every fifth camera frame or so.
Would appreciate any hints or code examples on how to achieve that. Thank you!
With logo detection, I would highly recommend using OpenCV HaarClassifier. It is easy to generate training samples from a collection of images of the logo, or one logo image with many distortions.
If you can use a few rules like the minimum and maximum size of the logo to be detected, and possible regions on the image where it can appear, you can run the detector at a speed better than you mention with ORB.

Multiple images in activity causing error

I'm developing an app that displays a step-by-step process for my company. Each step consist of one image and each activity shows eight to twelve images. I'm now running into the java.lang.outofmemory. It's also listing an android.view.InflateException:Binary xml error. I've resized the images to 360dpi and that only allowed a few more pics before reaching the error again. I also tried increasing the heap size, which did not change the result. Is there another way I should do this? Thanks a lot.
You can scale down the Image, thus reducing their memory footprint. Also, you could look into certain libraries like picasso and UIL and see if they can help you.
No magic here, i guess the imageview are rendering in full argb mode, so 8 bit * 4 * number of pixels. That's a lot!
How does your activity displays images ?
If they are in a list, make sure to use recycling.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html
You are interested in the ViewHolder
If you are using a ViewPager and swipes left/right, make sure to destroy your fragments while you are swiping, so that you can have in memory only 3 images (1 displayed, and 2 next to it).
If it's none of those, maybe you will have to refactor your layout :(
Last but not least, make sure to resize your bitmap to your exact screen size, as suggested before, Picasso provides a convenient mean to do it with .fit(). It will wait for the imageview to be measured, then resize the downloaded bitmap and fit it in the imageview. You don't even have to worry about networking on main thread, how wonderful !
Picasso.with(getActivity())
.load(imageUrl)
.fit()
.into(imageView);
This is where coding approach matters. How you've implemented it might help us pin point the memory leaks.
But a general practice ( maybe not good one)
Load all Bitmaps in a constant class. ( just once)
Refer infinitely.

Android: OutOfMemory while calling BitmapFactory.decodeStream()

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...

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.

Categories