快速拍照时应用程序崩溃

时间:2016-09-25 12:42:03

标签: android android-camera

在我的应用程序中,我有一个拍照并保存的按钮。不显示预览。

如果我每1-2秒点击一次按钮,该应用程序工作正常,但如果我点击它更快,应用程序崩溃。只要我没有太快点击按钮,我就可以拍摄我想要的所有照片。

PhotoFragment.java:

public class PhotoFragment extends Fragment {
    private final static String LOG_TAG = PhotoFragment.class.getSimpleName();
    private Camera mCamera;
    private SurfaceTexture mSurfaceTexture; // mSurfaceTexture must be global to avoid log error spam

    public static PhotoFragment newInstance() {
        Bundle args = new Bundle();
        PhotoFragment fragment = new PhotoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public PhotoFragment(){}

    @Override
    public void onStart() {
        super.onStart();
        getActivity().setTitle(R.string.photo);
    }

    @Override
    public void onResume() {
        super.onResume();
        safeCameraOpen();
    }

    @Override
    public void onPause() {
        super.onPause();
        releaseCamera();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_content, container, false);


        Button yo = (Button) rootView.findViewById(R.id.pic);
        yo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mCamera == null){
                    Log.w(LOG_TAG, "@onCreateView mCamera is null!");
                }
                mCamera.takePicture(null, null, mCall);
            }
        });

        Button yoyo = (Button) rootView.findViewById(R.id.vid);
        yoyo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        return rootView;
    }

    Camera.PictureCallback mCall = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            //decode the data obtained by the camera into a Bitmap and save it

            FileOutputStream outStream = null;
            try {
                String sdCardDirectory = Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                        .getPath();

                outStream = new FileOutputStream(sdCardDirectory
                        + Utils.getPictureFilename());

                outStream.write(data);
                outStream.close();
            } catch (FileNotFoundException e){
                Log.d("CAMERA", e.getMessage());
            } catch (IOException e){
                Log.d("CAMERA", e.getMessage());
            }
            mCamera.startPreview();
        }
    };

    private boolean safeCameraOpen() {
        boolean qOpened = false;
        try {
            releaseCamera();
            mCamera = Camera.open();
            mSurfaceTexture = new SurfaceTexture(Activity.MODE_PRIVATE);
            mCamera.setPreviewTexture(mSurfaceTexture);
            mCamera.startPreview();
            qOpened = (mCamera != null);
        } catch (Exception e) {
            Log.e(LOG_TAG, "failed to open Camera");
            e.printStackTrace();
        }
        return qOpened;
    }

    private void releaseCamera() {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }
    }


}

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.dcs.hiddencamera, PID: 10561
                                                                      java.lang.RuntimeException: takePicture failed
                                                                          at android.hardware.Camera.native_takePicture(Native Method)
                                                                          at android.hardware.Camera.takePicture(Camera.java:1434)
                                                                          at android.hardware.Camera.takePicture(Camera.java:1379)
                                                                          at com.dcs.hiddencamera.PhotoFragment$1.onClick(PhotoFragment.java:72)
                                                                          at android.view.View.performClick(View.java:5204)
                                                                          at android.view.View$PerformClick.run(View.java:21153)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:148)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

处理onClick中的runtimeexception,同时尝试禁用onclick中的按钮,并在保存图片后重新启用它。如果太快,相机显然无法处理两个连续的拍照命令。异常源位于本机摄像头代码中。