在我的 Android 应用程序中,我想从相机捕获图像并将该图像传递Uri
给下一个活动。
在这里,我得到了捕获图像的缩略图。为了解决这个问题,我使用MediaStore.EXTRA_OUTPUT
了我的图片捕捉意图。
现在,我不知道如何获取当前捕获图像的 uri。我尝试了下面的代码并传入imageUri
下一个活动,但它引发了NoSuchFileDirectory
异常。
我提到了这个链接,但对获取捕获的图像 Uri 并将其传递给下一个活动没有任何清晰的想法。请帮我解决这个问题。如果您发现此代码有任何问题,请建议我哪里出错了。
代码:
btnCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
Intent pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = null;
try {
photo = SaveCameraImage();
} catch (Exception e) {
Log.e("Camera capture-------------",
"Can't create file to take picture!");
Toast.makeText(SelectFrameActivity.this,
"Please check SD card! Image shot is impossible!",
10000).show();
}
mImageUri = Uri.fromFile(photo);
pictureActionIntent
.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
startActivityForResult(pictureActionIntent, TAKE_PICTURE);
}
});
private File SaveCameraImage() {
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dt = sdf.format(new Date());
imageFile = null;
imageFile = new File(Environment.getExternalStorageDirectory(),
"FrameFace/" + "Camera_" + dt + ".png");
Log.e("New Camera Image Path:-",
Environment.getExternalStorageDirectory() + "/FrameFace/"
+ "Camera_" + dt + ".png");
if (!imageFile.exists())
imageFile.createNewFile();
bmp_camera_image.compress(Bitmap.CompressFormat.PNG, 100,
new FileOutputStream(imageFile));
Log.e("imagetext", "the file has saved to the sdcard");
imagePath = Environment.getExternalStorageDirectory()
+ "/FrameFace/" + "Camera_" + dt + ".png";
Log.e("FaceFrameActivity - SaveCameraImage: ", "Image is Saved to"
+ imagePath);
} catch (Exception ex) {
ex.printStackTrace();
}
return imageFile;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
Log.e("result code: ", Integer.toString(Activity.RESULT_OK) + " "
+ Integer.toString(resultCode));
if (resultCode == Activity.RESULT_OK) {
// bmp_camera_image = (Bitmap) data.getExtras().get("data");
SaveCameraImage();
// this.getContentResolver().notifyChange(mImageUri, null);
// ContentResolver cr = this.getContentResolver();
// try {
// GlobalClass.cameraBitmap =
// android.provider.MediaStore.Images.Media
// .getBitmap(cr, mImageUri);
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
FaceCropActivity_dual.class);
// intent.putExtra("imagePath", imagePath);
intent.putExtra("imagePath", mImageUri.toString());
startActivity(intent);
}
}, 500);
}
break;
}
}