Java 从库中选择照片后应用程序崩溃

Java 从库中选择照片后应用程序崩溃,java,android,image-processing,Java,Android,Image Processing,我创建了一个图像处理应用程序,它使用openCV显示图像之间的相似性百分比。但是,每当我选择一个映像时,应用程序进程就会崩溃 以下是日志: E/HW-JPEG-DEC: [HME_JPEG_DEC_Delete](3321): HME_JPEG_DEC_Delete: decoder_ctx=null E/AndroidRuntime: FATAL EXCEPTION: main Process: softwareengineering.pwc.leafidentif

我创建了一个图像处理应用程序,它使用openCV显示图像之间的相似性百分比。但是,每当我选择一个映像时,应用程序进程就会崩溃

以下是日志:

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)
这是主活动161中的一行

toMatch[4] = BitmapFactory.decodeResource(getResources(), R.drawable.cleaf5);
你的文件太大了。 添加try catch块并尝试加载带有选项的较小图像。inSampleSize为选项尝试不同的值。inSampleSize

例如:

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
}

照片的高度和宽度有多大?测量信息不清楚java.lang.OutOfMemoryError:分配52985868字节失败?这张图片有52 MB,不适合你的记忆即使它似乎不符合任何原因关闭以外的印刷,因为OP没有阅读或理解日志,我会投票关闭这个。你没有足够的记忆力;它清楚地表明了这一点,我不是故意小气,但是,它就在那里说。如果问题是为什么你不能分配那么多,因为它不是很多RAM,那么请重写它。不过,它也清楚地告诉你,你只有4MB+15MB的内存可供使用,顺便说一句,所以如果它真的有助于解决这个问题的话,请拨打idk。如果你认为你的应用程序应该有超过19MB的可用内存,请给出手机上可用内存为1GB的所有原因,等等。