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();
}
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With