Titanium JIRA Archive
Appcelerator Community (AC)

[AC-2439] Android: OutOFMemory Errors when using image compression, resizing or thumbnails on select devices like the Galaxy S2

GitHub Issuen/a
TypeBug
Priorityn/a
StatusClosed
ResolutionCannot Reproduce
Resolution Date2013-09-17T23:46:41.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsandroid, camera, crash
ReporterYgor Lemos
AssigneeShak Hossain
Created2013-09-12T21:59:07.000+0000
Updated2016-03-08T07:41:29.000+0000

Description

When I try to use imageAsResized or imageAsThumbnail or imagefactory compress the app crashes with an OutOfMemory Error. Problem is that this crash happens on devices with 1GB RAM... I think this shouldn't happen for such devices on simple resizing operations. I use something like this for generating a thumb and compressing my photos before sending to my servers:
Ti.Media.showCamera({
    allowEditing: false,
    mediaTypes: Ti.Media.MEDIA_TYPE_PHOTO,
    saveToPhotoGallery: false,
    success: function(e) {
        
        var aspectRatio = e.media.height / e.media.width;
        photos[changeImage] = imagefactory.compress(e.media.imageAsResized(640, 640 * aspectRatio), 0.5);
        
        placeholder[changeImage].setImage(e.media.imageAsThumbnail(194, 0, 0));
    },
    error: function(e) {
        alert(e);
    }
});
This code runs fine on hundreds of devices but crashes on devices like the Samsung Galaxy SII (1GB RAM) and the LG L7 (512MB RAM) This is what I get on logcat:
09-12 18:07:39.633: I/Camera(11905): sendBroadcast intent.stop.app-in-app
09-12 18:07:42.613: W/IInputConnectionWrapper(11905): showStatusIcon on inactive InputConnection
09-12 18:07:42.933: D/PiniOn - UALib(11905): Push Service started with intent=Intent { act=com.urbanairship.push.START flg=0x4 cmp=br.com.pinion/com.urbanairship.push.PushService (has extras) }
09-12 18:07:53.443: D/webviewglue(11905): OnTrimMemory with EGL Context 0x4c291980
09-12 18:07:57.278: D/dalvikvm(11905): GC_FOR_ALLOC freed 2632K, 21% free 17099K/21511K, paused 81ms, total 81ms
09-12 18:07:57.278: I/dalvikvm-heap(11905): Forcing collection of SoftReferences for 31961104-byte allocation
09-12 18:07:57.393: D/dalvikvm(11905): GC_BEFORE_OOM freed 11K, 21% free 17088K/21511K, paused 113ms, total 115ms
09-12 18:07:57.393: E/dalvikvm-heap(11905): Out of memory on a 31961104-byte allocation.
09-12 18:07:57.393: I/dalvikvm(11905): "KrollRuntimeThread" prio=5 tid=11 RUNNABLE
09-12 18:07:57.393: I/dalvikvm(11905):   | group="main" sCount=0 dsCount=0 obj=0x41c37888 self=0x4c4c03f8
09-12 18:07:57.398: I/dalvikvm(11905):   | sysTid=11943 nice=0 sched=0/0 cgrp=apps handle=1280051272
09-12 18:07:57.398: I/dalvikvm(11905):   | schedstat=( 4318641709 3381257674 10368 ) utm=386 stm=45 core=1
09-12 18:07:57.403: I/dalvikvm(11905):   at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-12 18:07:57.408: I/dalvikvm(11905):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
09-12 18:07:57.408: I/dalvikvm(11905):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:724)
09-12 18:07:57.413: I/dalvikvm(11905):   at org.appcelerator.titanium.TiBlob.getImage(TiBlob.java:492)
09-12 18:07:57.413: I/dalvikvm(11905):   at org.appcelerator.titanium.TiBlob.imageAsResized(TiBlob.java:525)
09-12 18:07:57.413: I/dalvikvm(11905):   at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method)
09-12 18:07:57.413: I/dalvikvm(11905):   at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57)
09-12 18:07:57.413: I/dalvikvm(11905):   at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:43)
09-12 18:07:57.413: I/dalvikvm(11905):   at org.appcelerator.kroll.runtime.v8.V8Function$1.run(V8Function.java:70)
09-12 18:07:57.423: I/dalvikvm(11905):   at android.os.Handler.handleCallback(Handler.java:615)
09-12 18:07:57.423: I/dalvikvm(11905):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 18:07:57.433: I/dalvikvm(11905):   at android.os.Looper.loop(Looper.java:137)
09-12 18:07:57.433: I/dalvikvm(11905):   at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)
09-12 18:07:57.433: D/skia(11905): --- decoder->decode returned false
09-12 18:07:57.438: W/System.err(11905): java.lang.OutOfMemoryError
09-12 18:07:57.438: W/System.err(11905): 	at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-12 18:07:57.438: W/System.err(11905): 	at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:652)
09-12 18:07:57.448: W/System.err(11905): 	at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:724)
09-12 18:07:57.448: W/System.err(11905): 	at org.appcelerator.titanium.TiBlob.getImage(TiBlob.java:492)
09-12 18:07:57.448: W/System.err(11905): 	at org.appcelerator.titanium.TiBlob.imageAsResized(TiBlob.java:525)
09-12 18:07:57.453: W/System.err(11905): 	at org.appcelerator.kroll.runtime.v8.V8Function.nativeInvoke(Native Method)
09-12 18:07:57.453: W/System.err(11905): 	at org.appcelerator.kroll.runtime.v8.V8Function.callSync(V8Function.java:57)
09-12 18:07:57.453: W/System.err(11905): 	at org.appcelerator.kroll.runtime.v8.V8Function.call(V8Function.java:43)
09-12 18:07:57.463: W/System.err(11905): 	at org.appcelerator.kroll.runtime.v8.V8Function$1.run(V8Function.java:70)
09-12 18:07:57.463: W/System.err(11905): 	at android.os.Handler.handleCallback(Handler.java:615)
09-12 18:07:57.463: W/System.err(11905): 	at android.os.Handler.dispatchMessage(Handler.java:92)
09-12 18:07:57.463: W/System.err(11905): 	at android.os.Looper.loop(Looper.java:137)
09-12 18:07:57.463: W/System.err(11905): 	at org.appcelerator.kroll.KrollRuntime$KrollRuntimeThread.run(KrollRuntime.java:112)
09-12 18:07:57.583: D/webviewglue(11905): nativeDestroy view: 0x56af02e0
09-12 18:07:57.628: E/TiExceptionHandler(11905): (main) [111128,111432] ----- Titanium Javascript Runtime Error -----
09-12 18:07:57.628: E/TiExceptionHandler(11905): (main) [3,111435] - In form.js:561,69
09-12 18:07:57.628: E/TiExceptionHandler(11905): (main) [0,111435] - Message: Uncaught Error: Java Exception occurred
09-12 18:07:57.628: E/TiExceptionHandler(11905): (main) [0,111435] - Source:                 photos[changeImage] = imagefactory.compress(e.media.imageAsRes
09-12 18:07:57.843: W/ResourceType(11905): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
09-12 18:07:57.843: D/HardwareRenderer(11905): Profiling hardware renderer
09-12 18:07:57.853: E/V8Exception(11905): Exception occurred at form.js:561: Uncaught Error: Java Exception occurred
Any ideas on how to solve it? (I really don't want to enable largeHeap on the Manifest for this.)

Comments

  1. Motiur Rahman 2013-09-17

    Hi Ygor Lemos, Could you provide me full code so that i can reproduce this issue. Thanks,
  2. Ygor Lemos 2013-09-17

    Hi Motiur, The code is the one posted above... just compressing the image inside the success event of the showCamera gives the OutOfMemory error. I had to add "android:largeHeap" : "true" to the app AndroidManifest application tag in order for it to work properly on those devices...
  3. Mostafizur Rahman 2013-12-15

    We tried to reproduce this issue in-house with Titanium SDK 3.1.3.GA but were not able to reproduce. Please try this sample code and let us know if the issue persists.

    Testing Environment:

    Android SDK: 4.2.2 Titanium SDK: 3.1.3 Titanium CLI Version: 3.1.2 h5. Test Code
       var win = Ti.UI.createWindow({
       	backgroundColor : '#000',
       	layout : 'vertical',
       	navBarHidden : false,
       	title : 'Take a Photo From Camara'
       });
        
       var takePhoto = Ti.UI.createButton({
       	title : 'Take_Photo',
       	color : '#000',
       	width : Ti.UI.SIZE,
       	height : 60,
       	top : 10,
       });
        
       var image = Ti.UI.createImageView({
       	top : 10,
       	height : Ti.UI.SIZE,
       	width : Ti.UI.SIZE,
       });
        
       win.add(takePhoto);
       win.add(image);
       //win.add(anImageView);
       takePhoto.addEventListener('click', function() {
        
       	fireUpTheCamera();
       });
        
       function fireUpTheCamera() {
       	Titanium.Media.showCamera({
        
       		success : function(event) {
       			var cropRect = event.cropRect;
       			var photo = event.media;
        
       			Ti.API.debug('Our type was: ' + event.mediaType);
       			if (event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO) {
       				var aspectRatio = photo.height / photo.width;
        
       				//image.setImage(photo.imageAsThumbnail(129, 0, 0));
       				image.setImage(photo.imageAsResized(640, 600 * aspectRatio), 0.5);
        
       			} else {
       				alert("got the wrong type back =" + event.mediaType);
       			}
       		},
       		cancel : function() {
       		},
       		error : function(error) {
       			// create alert
       			var a = Titanium.UI.createAlertDialog({
       				title : 'Camera'
       			});
        
       			// set message
       			if (error.code == Titanium.Media.NO_CAMERA) {
       				a.setMessage('Please run this test on device');
       			} else {
       				a.setMessage('Unexpected error: ' + error.code);
       			}
        
       			// show alert
       			a.show();
       		},
       		saveToPhotoGallery : false,
       		allowEditing : false,
       		mediaTypes : [Ti.Media.MEDIA_TYPE_PHOTO]
       	});
       }
        
       win.open();
       

    Step to Test

    Create a sample Project

    Paste this code in app.js file

    Click the button and successfully take a photo with imageAsResized or imageAsThumbnail without any crash.

  4. Shak Hossain 2014-01-29

    Closing since we have not heard back from the reporter in a while.

JSON Source