当图片拍摄两次并在imageview中显示图像时,应用程序崩溃

时间:2013-08-09 15:11:56

标签: android out-of-memory

第一次,没有问题,它工作正常。但当我再次点击“拍照”时,应用程序崩溃了。这是我的代码:

ImageView myImage = (ImageView)findViewById(R.id.ivReturnPic);
Bitmap = bmap = BitmapFactory.decodeFile(file.getAbsolutePath());
myImage.setImageDrawable(bmap);

这是logcat信息:

08-09 10:06:23.835: E/AndroidRuntime(28492): FATAL EXCEPTION: main
08-09 10:06:23.835: E/AndroidRuntime(28492): java.lang.OutOfMemoryError
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:449)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at com.example.customcalenderandroid.Camera.onActivityResult(Camera.java:232)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.app.ActivityThread.access$1100(ActivityThread.java:140)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.os.Looper.loop(Looper.java:137)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at java.lang.reflect.Method.invoke(Method.java:511)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-09 10:06:23.835: E/AndroidRuntime(28492):    at dalvik.system.NativeStart.main(Native Method)
08-09 10:06:23.840: E/android.os.Debug(2401): !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error

1 个答案:

答案 0 :(得分:1)

您获得的异常是OutOfMemoryException。顾名思义,这意味着您的应用程序内存不足。在Android中处理图像有时会有点棘手,特别是在拍摄全尺寸照片时。没有多少智能手机,即使是高端智能手机,也可以处理多个同时加载的全尺寸位图。使用bitmap.recycle()尽快回收不需要的位图可以解决很多问题。在这里,您可以找到有关以内存友好方式加载位图的更多信息: http://developer.android.com/training/camera/photobasics.html#TaskScalePhoto