Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

take picture crashed on real device

I am using this code for take photos and in the emulator it is working correctly. It does not work on physical mobile devices and has an error on the Samsung Galaxy S4 and Sony Xperia Z2.

my code :

private void takePicture() {
    openCamera();
    camera.takePicture(new ShutterCallback() {
        @Override
        public void onShutter() {
        }
    }, new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
        }
    }, new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
            img_screenshot.setImageBitmap(bitmap);
            closeCamera();
        }
    });
}

private void openCamera() {
    camera = Camera.open();
    Camera.Parameters params = camera.getParameters();
    List<Size> sizes = params.getSupportedPictureSizes();
    Size mSize = sizes.get(0);
    params.setPictureSize(mSize.width, mSize.height);
    camera.setParameters(params);
}

private void closeCamera() {
    camera.release();
}
like image 267
Milad M Avatar asked Jul 22 '14 19:07

Milad M


People also ask

Why does my camera keep crashing?

Your device could have received an update (even a small one you didn't notice), and the camera app you have need to be updated to be compatible with your phone's update. Also, try to remember the last app you have installed before you started having this camera problem.

Why does my camera keep stopping on my phone?

Sometimes, the issue is your cache and data files. These files could be corrupt and the reason why your camera isnt working. To fix unfortunately, camera has stopped on Android, you should clear the camera cache and data files.


1 Answers

first be sure use permission in manifest

<uses-permission android:name="android.permission.CAMERA" />

after that before call takePicture, call startPreview like :

private void takePicture() {
    openCamera();
    camera.startPreview();
    camera.takePicture(new ShutterCallback() {

        @Override
        public void onShutter() {
        }
    }, new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
        }
    }, new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);
            img_screenshot.setImageBitmap(bitmap);
            closeCamera();
        }
    });
}

private void openCamera() {
    camera = Camera.open();
    Camera.Parameters params = camera.getParameters();
    List<Size> sizes = params.getSupportedPictureSizes();
    Size mSize = sizes.get(0);
    params.setPictureSize(mSize.width, mSize.height);
    camera.setParameters(params);
}

private void closeCamera() {
    camera.release();
}
like image 87
Emran Sadeghi Avatar answered Sep 21 '22 12:09

Emran Sadeghi