自撮りアプリを作ろうとしています。フロントカメラから画像をキャプチャして保存しました。しかし、ギャラリーまたはファイルマネージャーからの画像は開いていません。 「画像を読み込めません」というメッセージが表示されます。フラグをフロントカメラからバックカメラに変更すると、完全に機能します。
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private Context context;
private String TAG = "Preview";
public Preview(Camera mCamera, Context context) {
super(context);
this.mCamera = mCamera;
this.context = context;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
@Override
public void surfaceCreated(SurfaceHolder mHolder) {
safeCameraOpen();
if (mCamera != null) {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
mCamera.release();
mCamera = null;
}
} else {
Log.d(TAG, "Error in camera : ");
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if (mCamera != null) {
setCameraDisplayOrientation((Activity) context, mCamera);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
releaseCameraAndPreview();
}
// This method is called while clicking for selfie
public void captureImage() {
mCamera.takePicture(null, null, mPicture);
}
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
saveImage(data);
}
};
public void saveImage(byte[] data) {
try {
File imageFile = createImageFile();
if (imageFile != null) {
FileOutputStream fos = new FileOutputStream(imageFile);
fos.write(data);
fos.close();
Toast.makeText(context, "New Image saved", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Directory problem", Toast.LENGTH_LONG).show();
}
} catch (Exception error) {
error.getMessage();
Toast.makeText(context, "Image could not be saved.", Toast.LENGTH_LONG).show();
}
}
private File createImageFile() throws IOException {
// Create an image file name
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Selfie");
if (!storageDir.exists() && !storageDir.mkdirs()) {
Log.d(TAG, "Can't create directory to save image.");
Toast.makeText(context, "Can't create directory to save image.", Toast.LENGTH_LONG).show();
return null;
}
File image = File.createTempFile(String.valueOf(System.currentTimeMillis()), /* prefix */ ".jpg", /* suffix */ storageDir /* directory */);
return image;
}
private boolean safeCameraOpen() {
boolean qOpened = false;
try {
releaseCameraAndPreview();
mCamera = openFrontFacing();
qOpened = (mCamera != null);
} catch (Exception e) {
e.printStackTrace();
}
return qOpened;
}
public void releaseCameraAndPreview() {
// mPreview.setCamera(null);
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
private Camera openFrontFacing() {
int cameraCount = 0;
Camera camera = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
camera = Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return camera;
}
}
openFrontFacing()
メソッドに変更CAMERA_FACING_FRONT
するCAMERA_FACING_BACK
と、完全に機能します。