I need to get the File path Uri of the image after the user takes a photo using SurfaceView in Android. I need this so that I can display the image in another view.
Here's my current code:
captureImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, jpegCallback);
}
});
mSurfaceView.getHolder().addCallback(this);
mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
try {
Intent i = new Intent(CameraActivity.this, ImageEditor.class);
//i.putExtra("image", I_need_to_send_the_Uri);
startActivityForResult(i, 0);
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mSurfaceView.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width, selected.height);
mCamera.setParameters(params);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
How do I get the Image Uri from it? Or what other method do you suggest to open the image in another view? I tried sending the byte[] array and the bitmap but it's way too heavy to be transferred between views and that's bad practice. Help out if you can :)
First of all you need to save the data in to file and store it in to device storage.
PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
Get Uri from file by this code
Uri.fromFile(Filename);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments