problem with taking pictures using the android camera

自闭症网瘾萝莉.ら 提交于 2019-12-29 07:16:25

问题


I have an app where I'm using the android camera to take pictures.I have built my own android camera.And pictures are taken by pressing a button.

Something like this:

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


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

        }
    }
};

Once the picture is taken I use an intent and send the bytes to another activity.

The big problem is that if I take several photos, one by one by pressing the button, my app crashes at this line:

mCamera.takePicture(null, mPictureCallback, mPictureCallback);

This is how my logcat looks like:

java.lang.RuntimeException: takePicture failed
at android.hardware.Camera.native_takePicture(Native Method)
at android.hardware.Camera.takePicture(Camera.java:746)
at android.hardware.Camera.takePicture(Camera.java:710)
at com.Xperiaproject.TakePhoto.onClick(TakePhoto.java:216)
at android.view.View.performClick(View.java:2534)
at android.view.View$PerformClick.run(View.java:9210)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
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)

Any idea how to solve this?

EDIT: I've also tried:

mCamera.takePicture(null, null, mPictureCallback);

but crashes after several pictures!!!

!!!!!!!!This is my whole code: http://pastebin.com/0U1pQSak


回答1:


Make sure that you call Camera.startPreview() again after you have taken a picture.

8) After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.

from the Camera class documentation




回答2:


I had a similar problem in this thread and managed to solve it by adding a System.gc() just before calling takePicture().

System.gc();
CameraParameters.mCamera.takePicture(null, null, jpegCallback);



回答3:


Here is the code that I have used and it works https://github.com/commonsguy/cw-advandroid/blob/master/Camera/Picture/src/com/commonsware/android/picture/PictureDemo.java



来源:https://stackoverflow.com/questions/7258473/problem-with-taking-pictures-using-the-android-camera

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