使用Camera Intent时出现OutOfMemoryError

时间:2014-01-02 01:56:05

标签: android bitmap out-of-memory android-camera-intent

我正在使用相机Intent拍摄照片并将其填充到ImageView。每当我从相机Intent拍摄另一张照片时,我都会收到错误。当我只拍摄一张照片但第二次尝试而不杀死应用程序时,它会正常工作,会弹出OutOfMemory错误。

这是logcat

01-02 09:51:39.930: E/AndroidRuntime(31339): FATAL EXCEPTION: main
01-02 09:51:39.930: E/AndroidRuntime(31339): java.lang.OutOfMemoryError
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:650)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:722)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:788)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at com.example.photosharingtest2.MainActivity.onActivityResult(MainActivity.java:218)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.access$1100(ActivityThread.java:140)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.os.Looper.loop(Looper.java:137)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invokeNative(Native Method)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at java.lang.reflect.Method.invoke(Method.java:511)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-02 09:51:39.930: E/AndroidRuntime(31339):    at dalvik.system.NativeStart.main(Native Method)

这就是我用相机拍摄照片所做的。

btn_takeImage = (Button) findViewById(R.id.btn_takeImage);
    btn_takeImage.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // Intent camera_intent = new
            // Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);

            values = new ContentValues();
            values.put(MediaStore.Images.Media.TITLE, "New Picture");
            values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
            imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
            startActivityForResult(intent, PICTURE_RESULT);
        }
    });
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    switch (requestCode)
    {
        case PICTURE_RESULT:
            if (requestCode == PICTURE_RESULT)
                if (resultCode == Activity.RESULT_OK)
                {
                    try
                    {
                        thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);

                        img_backgroundImage.setImageBitmap(thumbnail);
                        imageurl = getRealPathFromURI(imageUri);
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            break;
    }
}

2 个答案:

答案 0 :(得分:1)

关闭OOM的

第一次可能是由大图片造成的,您应该查看此文档,看看是否可以加载缩小尺寸的图片:Loading Large Bitmaps Efficiently

其次thumbnail实例引用?在这种情况下,我建议你改为使用局部变量(在方法完成后让它超出范围),因为我猜你把它挂在第二张图像上,因此你的应用程序占用的内存量翻了一倍(因为在设置新图像之前不会取消引用旧引用。)

在代码更改中:

thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
img_backgroundImage.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);

为:

Bitmap thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
img_backgroundImage.setImageBitmap(thumbnail);
imageurl = getRealPathFromURI(imageUri);

答案 1 :(得分:0)