java.lang.OutOfMemory Exception

时间:2014-01-29 04:58:48

标签: android bitmap

获取 java.lang.OutOfMemory 异常 - 位图

我知道 inSampleSize 将帮助我解决我的问题,但有点混淆如何在我的代码中使用它。

例外行: -

bMapRotate = Bitmap.createBitmap(tempBM, 0, 0, tempBM.getWidth(),

showPicture()方法:

private void showPicture(){
        if(pic!=null) tempBM = BitmapFactory.decodeFile(pic);
        Bitmap bMapRotate;
        Matrix matrix = new Matrix();
        if(info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT){
            float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
            Matrix matrixMirrorY = new Matrix();
            matrixMirrorY.setValues(mirrorY);
            matrix.postConcat(matrixMirrorY);
            bMapRotate = Bitmap.createBitmap(tempBM, 0, 0, tempBM.getWidth(),
            tempBM.getHeight(), matrix, true);

        }

        if(Utility.isTablet(this)) {
             int orient = getResources().getConfiguration().orientation;

            if(orient==1){
                matrix.postRotate(90);
                bMapRotate = Bitmap.createBitmap(tempBM, 0,0,tempBM.getWidth(),
                tempBM.getHeight(), matrix ,true);
                tempBM = bMapRotate;
            }

        }else{
            //Get Orientation:
            int orientation;

            if(tempBM.getHeight() < tempBM.getWidth()){
                orientation = 90;
            } else {
                orientation = 0;
            }
            if (orientation != 0) {
                matrix.postRotate(orientation);
                bMapRotate = Bitmap.createBitmap(tempBM, 0, 0, tempBM.getWidth(),
                        tempBM.getHeight(), matrix, true);

            } else
                bMapRotate = Bitmap.createScaledBitmap(tempBM, tempBM.getWidth(),
                        tempBM.getHeight(), true);

            tempBM = bMapRotate;
        }

日志: -

01-29 10:16:04.625: E/AndroidRuntime(20234): java.lang.OutOfMemoryError
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.graphics.Bitmap.nativeCreate(Native Method)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.tanukiteam.camera.CameraAPIActivity.showPicture(CameraAPIActivity.java:685)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.tanukiteam.camera.CameraAPIActivity.access$1(CameraAPIActivity.java:650)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.tanukiteam.camera.CameraAPIActivity$1.onPictureTaken(CameraAPIActivity.java:539)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:789)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.os.Looper.loop(Looper.java:137)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at android.app.ActivityThread.main(ActivityThread.java:4921)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at java.lang.reflect.Method.invokeNative(Native Method)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at java.lang.reflect.Method.invoke(Method.java:511)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1036)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:803)
01-29 10:16:04.625: E/AndroidRuntime(20234):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

Bitmaps占用大量内存,特别是大内存。在将其加载到内存之前,您很可能需要缩小BitmapThis tutorial是如何实现这一目标的一个很好的例子。

答案 1 :(得分:1)

我认为这是因为你的tempBM太大而无法存储在内存中。解码资源文件时可以使用BitmapFactory.Options来缩小位图。见this

答案 2 :(得分:0)

首先通过捕获您认为可能产生此错误的地方的OutOfMemory错误来阻止您的应用程序在运行时崩溃,如下所示:

try {

...

}
catch(OutOfMemoryError error)  {
    //decide what to do when there is not more memory available
}

(OR)

Runtime.getRuntime().gc();

调用垃圾收集器是一个好主意。