Processing: FATAL EXCEPTION: Animation Thread java.lang.OutOfMemoryError (Android mode) - android

I am running an android app on my device using processing. But I get this error:
FATAL EXCEPTION: Animation Thread
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:592)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:691)
at processing.core.PApplet.loadImage(Unknown Source)
at processing.test.djtubedesktop.DJTubeDesktop.loadImages(DJTubeDesktop.java:972)
at processing.test.djtubedesktop.DJTubeDesktop.setup(DJTubeDesktop.java:62)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PGraphicsAndroid2D.requestDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:856)
I have tried this:
1) Going to File -> preferences, and select "then Increase maximun available memory to" 10240 MB
2) Reducing the number of frames.
I have this 2 code lines:
images = loadImages("Animation_data/movie", ".jpg", 134);
recordPlayer = loadImages("black-record_", ".png", 36);
I have replaced 134 and 36 for 50 and 10, and by doing this I dont get the error, but it still does not work
The programs runs perfectly on java
I need to run it on my android device

The image you are manipulating is too large for Android. You are running out of Bitmap memory Try making the image smaller. You might want to check this link.

OutOfmemory Exception :-
this is because the image size is heavy.try using this code.
Bitmap bitmapimagesize = Bitmap.createScaledBitmap(your bitmap,
yourbitmap.getWidth() / 5, yourbitmap.getHeight() / 5,
false);
or this:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
bitmap = BitmapFactory.decodeFile(image_path, options);

Related

Codename one handling image memory size?

My Senario is the following , I have a form where I need the user to attach images that he takes from the camera [count vary depending on forms -I have many cases- ]
the application display an alert of no available memory [ on some devices , I have seen it on (Samsung A5 [2015 release] , Xiaomi Note 4)
Exception: java.lang.OutOfMemoryError - Failed to allocate a 16894 byte allocation with 11344 free bytes and 11KB until OOM
and some times on loading the map
Process: com.x.y, PID: 17227
java.lang.OutOfMemoryError: Failed to allocate a 65548 byte allocation with 14824 free bytes and 10KB until OOM
at com.android.okhttp.okio.Segment.<init>(Segment.java:62)
at com.android.okhttp.okio.SegmentPool.take(SegmentPool.java:46)
at com.android.okhttp.okio.Buffer.writableSegment(Buffer.java:1114)
at com.android.okhttp.okio.Buffer.write(Buffer.java:940)
at com.android.okhttp.okio.RealBufferedSink$1.write(RealBufferedSink.java:197)
at java.io.DataOutputStream.write(DataOutputStream.java:107)
at java.io.FilterOutputStream.write(FilterOutputStream.java:97)
at ez.a(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):15)
at ez.a(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):83)
at ep.b(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):2)
at eo.a(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):4)
at eq.a(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):55)
at com.google.maps.api.android.lib6.drd.ap.a(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):11)
at dx.a(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):17)
at dx.run(:com.google.android.gms.dynamite_mapsdynamite#13280047#13.2.80 (040308-211705629):65)
the code that is used to capture and take the image and store it array of images
String filePath = Capture.capturePhoto();
applicationAttachment[index] = temp;
applicationAttachment[index].setImageName(labelName.substring(0, labelName.length()-"Label".length()));
My question here , how can I handle the image based on available memory if that is possible ??

why android asks for a huge memory allocation for a small photo?

I am trying to load a jpeg photo with a size of 965KB on my android phone. The code is below. When I run the code, the app crashes.
Button _btn = (Button) findViewById(R.id.btn1);
_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Drawable _draw = getResources().getDrawable(R.drawable.sea, null);
}
});
This is the crash log.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.a3sumatch.multipart, PID: 29240
java.lang.OutOfMemoryError: Failed to allocate a 362797068 byte
allocation with 8388608 free bytes and 254MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at
android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at
android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)
at
android.content.res.Resources.loadDrawableForCookie(Resources.java:2761)
at android.content.res.Resources.loadDrawable(Resources.java:2654)
at android.content.res.Resources.getDrawable(Resources.java:833)
at
com.a3sumatch.multipart.MainActivity$1.onClick(MainActivity.java:40)
at android.view.View.performClick(View.java:5205)
at android.view.View$PerformClick.run(View.java:21176)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5611)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
I am trying to load a jpeg photo with a size of 965KB on my android phone
That is an absolutely massive photo. JPEG, like PNG and WebP, is a compressed file format. To put that in perspective, a 3229x2480 photo of my balding head is 829.3KB in a JPEG. I would expect yours to be a bit larger in each dimension. That resolution is higher than the resolution of any Android device screen that I know of.
Your OutOfMemoryError is for a 362797068-byte allocation. A Bitmap is an uncompressed version of the image. 362797068 bytes is equivalent to about a 9524x9524 square image (and 4 bytes/pixel).
So, you need to do two things:
If you have this image in res/drawable/, move it to res/drawable-nodpi/
Reduce the resolution of this image by at least a factor of four along each dimension

Android Renderscript Allocation.USAGE_SHARED crash

I am getting a crash while running my app which uses renderscript. Unfortunately, the logcat does not give any specific details.
b = Bitmap.createBitmap(ib.getWidth(), ib.getHeight(),ib.getConfig());
Allocation mInAllocation = Allocation.createFromBitmap(mRS, inBitmap,
Allocation.MipmapControl.MIPMAP_NONE,Allocation.USAGE_SHARED);
Allocation mOutAllocation2 = Allocation.createFromBitmap(mRS,
outBitmap, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SHARED);
...execute an algorithm from .rs file and later do the below
mOutAllocation2.copyTo(outBitmap)`;
The same code sequence runs perfectly fine, when I used USAGE_SCRIPT flag instead of USAGE_SHARED for mOutAllocation2.
Any help on why this could happen?
I read in android docs that if the allocation is of the type USAGE_SHARED, then the copy operation from allocation to the bitmap (see above) is faster.
Currently, I am seeing copies from allocation to bitmaps running into secs for decently large images (8MP and above)
I am using Nexus 10 (Android 4.3) currently.
First, you need to be using Allocation.USAGE_SCRIPT | Allocation.USAGE_SHARED. createFromBitmap(RenderScript, Bitmap) will set that for you when possible.
Second, if your copy times are taking that long, you're probably seeing script execution as well. Script execution is asynchronous, so the wall clock time of copyTo(Bitmap) may include significantly more than just the copy.
I was facing the same problem and I resolved it, this issue was happening because my bitmap configuration was not Bitmap.Config.ARGB_8888, we should convert it to ARGB_8888 before applying the blur.
Bitmap U8_4Bitmap;
if (yourBitmap.getConfig() == Bitmap.Config.ARGB_8888) {
U8_4Bitmap = yourBitmap;
} else {
U8_4Bitmap = yourBitmap.copy(Bitmap.Config.ARGB_8888, true);
}

Out of Memory Error while loading bitmaps

i have an android app with 3 acitivtys:
A1 --starts--> A2 --starts--> A3 --when finished his process: starts--> A1
(so i don't "finish();" an app. i start the next activitys with "startActivity(..);" the whole time after userinteraction)
so there is a loop in these 3 activitys.
On each Activity, i display 3-9 pictures, located on the SD-card, which i load with my following function:
try
{
Uri selectedImageURI = Uri.parse(strImagePath);
File imgFile = new File(getRealPathFromURI(selectedImageURI, c));
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ivTmp.setImageBitmap(myBitmap);
}catch (Exception e)
{
return null;
}
This all works.
But sometimes (after looping a few times through my activitys), my app crashes..
Logcat tells me:
01-16 13:42:15.863: DEBUG/dalvikvm(23161): GC_BEFORE_OOM freed 10K, 9% free 59019K/64400K, paused 29ms, total 30ms
01-16 13:42:15.863: ERROR/dalvikvm-heap(23161): Out of memory on a 8018704-byte allocation.
01-16 13:42:15.863: ERROR/AndroidRuntime(23161): FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785)
at android.content.res.Resources.loadDrawable(Resources.java:1965)
at android.content.res.Resources.getDrawable(Resources.java:660)
at android.widget.ImageView.resolveUri(ImageView.java:616)
at android.widget.ImageView.setImageResource(ImageView.java:349)
at <MyApp>.MyActivity$6.run(MyActivity.java:143)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5039)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Someone can give my some tips how to handle the crashes?
Maybe its because my activitys are set to state "paused" instead of closing them correctly?
for quick fix you can add
android:largeHeap="true" in your manifest application tag
link here:
I face OOM problem for my table in kindle and nook
Heap size (large)
android:largeHeap="true"
there specification is mentioned to use larger heap link here:
edit: use Caching Bitmaps technique link here
I had the similar problem while displaying high resolution images. I have searched and applied all the android bitmap solutions given in http://developer.android.com/training/displaying-bitmaps/index.html and the following caches mekanism in the link. but none of them work. not any right solution anywhere. Then I figured out that the problem is : I couldnt use the drawable folder structure right. I was keeping high resolution images in mdpi and hdpi folders . this cause android scale up the images to the ultra sizes. 100 kb image was causing 20 mb increase in memory thanks to the android monitor / memory section.
so I have added these ultra resolution images to xxhdpi folder , then It was FIXED suddenly. then my image slider work flawlessly
Yeah android doesnt immediately destroy activities when they are not visible to the user, but there are a number of lifecycle methods that are called depending on the current state. Check out http://developer.android.com/guide/components/activities.html#Lifecycle
You could try finishing the activities in onStop() or onDestroy() as these will be called when the activity is not visible and when the system runs low on memory respectively. :-)
This is due to the high resolution of image you have to scaling the bitmap use the following code
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
int h = 100; // height in pixels
int w = 100; // width in pixels
Bitmap photoBitMap = Bitmap.createScaledBitmap(myBitmap,h, w, true);
ivTmp.setImageBitmap(photoBitMap);

How to work with a lot of bitmaps and avoid the error: "bitmap size exceeds VM budget"? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Android: Strange out of memory issue
I'm trying to work with four bitmaps of 1024 x 1024, I need to have four in memory because I am creating a gallery that must show four zoomable bitmaps in landscape mode.
It is working on Motorola Droid 2.2.3, but it is failing on galaxy spica (2.1). This is the exception that i get on spica:
02-28 15:45:25.963: ERROR/dalvikvm-heap(17109): 4194304-byte external allocation too large for this process.
02-28 15:45:25.968: ERROR/(17109): VM won't let us allocate 4194304 bytes
02-28 15:45:25.973: DEBUG/AndroidRuntime(17109): Shutting down VM
02-28 15:45:25.973: WARN/dalvikvm(17109): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
02-28 15:45:25.973: ERROR/AndroidRuntime(17109): Uncaught handler: thread main exiting due to uncaught exception
02-28 15:45:25.988: ERROR/AndroidRuntime(17109): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
how to solve this?
thanks
There is no easy way to solve that - single application has very low memory budget, so there is no way to keep 16MB in memory. You can use OpenGL (textures are not included into that budget), of just use one bitmap at time (or use smaller bitmaps).
You can cut the size in half by using RGB_565 versus the default ARGB_8888.
ARGB_8888 requires 4 bytes per pixel: 4*1024*1024 = 4194304 (4mb)
RGB_565 require 2 bytes per pixel: 2*1024*1024 = 2097152 (2mb)
You can set this with options to the BitmapFactory. See inPreferredConfig
However even with this you'll still have difficulty. Why not cut the size of the images down as well? I doubt you're able to show a 1024x1024 image anyway.

Categories

Resources