我需要在用户使用Android中的SurfaceView拍照后获取图像的文件路径Uri。我需要这个,以便我可以在另一个视图中显示图像。
这是我目前的代码:
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();
}
如何从中获取Image Uri?或者你建议在另一个视图中打开图像的其他方法是什么?我尝试发送byte []数组和位图,但它太重了,无法在视图之间传输,这是不好的做法。如果可以,请帮帮忙:))
答案 0 :(得分:3)
首先,您需要将数据保存到文件中并将其存储到设备存储中。
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;
}
通过此代码从文件中获取Uri
Uri.fromFile(Filename);