When I want to get the image uri after I captured a photo, it always got nothing, but after I captured again, it returns the first one, not the current one. It seems like it always get the one before the latest. The following is my camerapreview code:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri outputMediaFileUri;
private String outputMediaFileType;
public File pic;
public Uri uri;
public CameraPreview(Context context) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
}
private static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
Log.d(TAG, "camera is not available");
}
return c;
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = getCameraInstance();
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
mHolder.removeCallback(this);
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
int rotation = getDisplayOrientation();
mCamera.setDisplayOrientation(90);
}
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), TAG);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"prescription" + ".jpg");
outputMediaFileType = "image/*";
} else {
return null;
}
outputMediaFileUri = Uri.fromFile(mediaFile);
return mediaFile;
}
public Uri getOutputMediaFileUri() {
return outputMediaFileUri;
}
public String getOutputMediaFileType() {
return outputMediaFileType;
}
public void takePicture() {
mCamera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
Log.i("camera", "file done");
if (pictureFile == null) {
Log.d(TAG, "Error creating media file, check storage permissions");
return;
}
try {
Log.i("camera", "do outputstream");
FileOutputStream fos = new FileOutputStream(pictureFile);
Log.i("camera", "after outputstream");
s = getOutputMediaFileUri().toString();
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d(TAG, "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
});
}
public int getDisplayOrientation() {
Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
android.hardware.Camera.CameraInfo camInfo =
new android.hardware.Camera.CameraInfo();
android.hardware.Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);
int result = (camInfo.orientation - degrees + 360) % 360;
return result;
}
}
After I call camerapreview.takepicture(), I tried to call camerapreview.outputMediaFileUri.toString(), but it doesn't work. Can someone help me?
Updated: I found that in the main activity, mPreview.takePicture() is always run at the end of all the precedures:
Button buttonCapturePhoto = (Button) findViewById(R.id.button_capture_photo);
buttonCapturePhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mPreview.takePicture();
path = mPreview.geturi;
goToFourthActivity(path);
}
}
});
At each time, "path" will read the image uri FIRST, and then do mPreview.takePicture(). So it would always be the previous uri, I don't know why, how can I fix it?
Your code
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"prescription" + ".jpg");
unconditionally reuses the same file name. No wonder you get the same URI.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.