在我的Android应用程序中,我想从相机捕获图像并将该图像Uri
传递给下一个活动。
在这里,我正在获取捕获图像的缩略图。为了解决这个问题,我使用MediaStore.EXTRA_OUTPUT
作为我的图片捕获意图。
现在,我不知道如何获取当前捕获图像的uri。我尝试了下面的代码并在下一个活动中传递了imageUri
,但它抛出了NoSuchFileDirectory
异常。
我提到了this链接,但没有明确了解如何获取捕获的图像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;
}
}
答案 0 :(得分:2)
我使用了这段代码,它为我工作,以保存相机图像和获取该图像。
btnCamera.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
dialog.dismiss();
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();
imagePath = Environment.getExternalStorageDirectory()
+ "/FrameFace/" + "Camera_" + dt + ".png";
Uri outputFileUri = Uri.fromFile(imageFile);
Intent pictureActionIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pictureActionIntent.putExtra(MediaStore.EXTRA_OUTPUT,
outputFileUri);
startActivityForResult(pictureActionIntent, TAKE_PICTURE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
@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) {
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(getApplicationContext(),
FaceCropActivity_dual.class);
// intent.putExtra("imagePath", imagePath);
Log.e("Image Path-------------", "" + imagePath);
intent.putExtra("imagePath", imagePath);
startActivity(intent);
}
}, 500);
}
break;
}
}
答案 1 :(得分:0)
imageFile = new File(Environment.getExternalStorageDirectory(),
"FrameFace/" + "Camera_" + dt + ".png");
这里是您的创建子目录和一次性文件。你创建目录然后图像文件
<强>更新强> 试试这个
imageFile = new File(Environment.getExternalStorageDirectory()+"/FrameFace" ,"Camera_" + dt + ".png");
从oncick监听器
中删除它 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();
}
答案 2 :(得分:0)