在我的应用程序中,我正在通过相机拍摄照片并在ImageView中显示。我面临的问题是,当我拍照并使用完()后退按钮关闭应用程序,然后重新打开我的应用程序,然后当我拍摄另一张照片时,显示的图像是上一张图像。
这是我的代码:
imv_camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CarrierDETAILS.from_cameraORgallery = true;
if (CarrierDETAILS.imageNumber < 5) {
//curr_ImageView=ar_imvID[CarrierDETAILS.imageNumber];
cameraORgallery = "camera";
String fileName = "fkd.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION, "Image capture by camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
} else {
Toast.makeText(SellActivity_2.this, "Only 5 Images can be uploaded.", Toast.LENGTH_SHORT).show();
}
}
});
在OnActivityResult中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
String imageId = convertImageUriToFile(imageUri, CameraActivity);
try {
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageId);
String filePath = getRealPathFromURI(SellActivity_2.this, uri);
CarrierDETAILS.arl_imagePath.add(filePath);
System.out.println("path from URI " + filePath);
CarrierDETAILS.from_cameraORgallery = true;
/************** Decode an input stream into a bitmap. *********/
if (bitmap != null) {
bitmap.recycle();
bitmap = null;
}
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
if (bitmap != null) {
mBitmap = bitmap;
}
} catch (IOException e) {
// Error fetching image, try to recover
/********* Cancel execution of this task. **********/
e.printStackTrace();
cancel(true);
}
}
}
}
这里imageId是一个全局Uri变量(在OnClickListener和OnActivityResult中使用)。起初我用过
Uri selectedImageUri = data.getData();
但我总是变空。 我想要的是获取图像的Uri对象或刚刚捕获的图像的文件路径。
非常感谢任何帮助;