使用Android相机拍摄几张照片后出错

时间:2011-08-31 11:36:25

标签: android bitmap out-of-memory

我有activity A我正在建造自己的相机。在此活动中,相机被打开,当使用按下按钮时,会拍摄照片。

这样做是这样的:

Activity A:
  //this button needs to be pressed to take the photo

  public void onClick(View arg0) {
  mCamera.takePicture(null, mPictureCallback, mPictureCallback);
            }

  //the method that gets called
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    public void onPictureTaken(byte[] imageData, Camera c) {

        if (imageData != null) {
            Intent mIntent = new Intent();


            Bundle b = new Bundle();
            b.putByteArray("imageData", imageData);
            Intent i = new Intent(mContext, ViewPhoto.class);
            i.putExtras(b);
            startActivity(i);

            setResult(FOTO_MODE, mIntent);
            finish();

        }
    }
};

图片被发送到第二个活动,用户可以在其中查看它是否喜欢它。如果他不喜欢它,他可以重新拍摄照片

活动B:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
                //..........
       Bundle extras = getIntent().getExtras();
       BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    byte[] imageData = extras.getByteArray("imageData");
    Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
            imageData.length, options);

    Matrix mat = new Matrix();
    mat.postRotate(90);
    bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
            myImage.getHeight(), mat, true);

               //.......
                 } 

在第二个活动中,我收到图片的字节并创建第一个bitmap

 Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
                imageData.length, options);

之后我需要旋转first bitmap创建第二个位图:

bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
                myImage.getHeight(), mat, true);

用户看到bitmapResult后,他可以重新拍摄照片。

backTakePhoto.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            //...back to activity A
        }
    });

问题是拍完几张照片之后,循环如下:

 activity A->activity B->activity A->Activity B my app crashes in activity B at this line:

bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
                myImage.getHeight(), mat, true);

这就是我的logcat的样子:

 FATAL EXCEPTION: main
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
at com.Xperiaproject.ViewPhoto.onCreate(ViewPhoto.java:71)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
at android.app.ActivityThread.access$1500(ActivityThread.java:121)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
at dalvik.system.NativeStart.main(Native Method)
Force finishing activity com.Xperiaproject/.ViewPhoto

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

您没有足够的内存来创建位图。这就是崩溃的原因。

为了释放你的记忆,你应该在未使用时回收你的位图,或者将它保存到SD卡然后再循环。

bitmapResult.recycle();

答案 1 :(得分:2)

在按钮上单击事件编写以下代码。

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

onActivityResult方法: -

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                System.out.println("Dipak Keshariya");
                drawable=null;
                bmpImage=null;
                bmpImage = (Bitmap) data.getExtras().get("data");
                System.out.println("Image Path is:- " +data.getExtras().get("data"));
                drawable = new BitmapDrawable(bmpImage);
                mImageviewmain.setVisibility(View.VISIBLE);
                mImageviewmain.setImageDrawable(drawable);
            }
        }
}

全球声明跟随变量/对象: -

位图bmpImage;

Drawable drawable;