error after taking several pictures using the android camera

╄→гoц情女王★ 提交于 2019-12-11 01:59:18

问题


I have an activity A where I'm building my own camera.In this activity the camera is opened and when the use presses a button a picture is taken.

This is done like this:

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();

        }
    }
};

The picture is sent to a second activity where the user can view it and see if it likes it or not.If he doesn't likes it he can then retake the photo

Activity 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);

               //.......
                 } 

In this second activity I receive the bytes of the picture and create a first bitmap:

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

After that I need to rotate the first bitmap creating a second bitmap:

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

After the user sees bitmapResult he can retake the photo.

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

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

The problem is that after taking several photos, loops like:

 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);

And this is how my logcat looks like:

 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

Any ideas?


回答1:


You don't have enough memory to creat your bitmap. That's why it crashes.

To free your memory you should recycle your bitmap when unused or save it to the sd card then recycle.

bitmapResult.recycle();



回答2:


Write Following Code on Button Click Event.

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

onActivityResult Method:-

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);
            }
        }
}

Declare Following Variable/Objects Globbally:-

Bitmap bmpImage;

Drawable drawable;



来源:https://stackoverflow.com/questions/7256362/error-after-taking-several-pictures-using-the-android-camera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!