Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - App crashing after selecting photo from gallery

I created an image processing app which shows percentage of similarity between images using openCV. However, whenever I select an image the application process crashes.

Here is the logcat:

E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: softwareengineering.pwc.leafidentifierv2, PID: 1036
              java.lang.OutOfMemoryError: Failed to allocate a 52985868 byte allocation with 4194304 free bytes and 15MB until OOM
                  at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
                  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:701)
                  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:508)
                  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:541)
                  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:512)
                  at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:572)
                  at softwareengineering.pwc.leafidentifierv2.MainActivity.onActivityResult(MainActivity.java:161)
                  at android.app.Activity.dispatchActivityResult(Activity.java:7193)
                  at android.app.ActivityThread.deliverResults(ActivityThread.java:4280)
                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:4327)
                  at android.app.ActivityThread.-wrap22(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1624)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:156)
                  at android.app.ActivityThread.main(ActivityThread.java:6523)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

and here is the line in mainactivity 161

toMatch[4] = BitmapFactory.decodeResource(getResources(), R.drawable.cleaf5);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your file is too big. Add try-catch block and try to load smaller images with options.inSampleSize (try different values for options.inSampleSize)

Example:

Bitmap bitmap = null;

try{
bitmap =  BitmapFactory.decodeResource(getResources(), R.drawable.cleaf5, options);
}catch(OutOfMemoryError e)    
   try{
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inSampleSize = 8;
   bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cleaf5, options);
   }catch(OutOfMemoryError e) {}
}

if(bitmap != null){
//do smth
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...