我一直在使用相机意图来捕捉图片。第一次它完美地工作,我能够捕捉图像并在ImageView中显示。但是当我尝试第二次捕捉不同的图像时,它仍然显示相同的旧图像。
这是我的相机意图和onActivityResult()代码:
Uri selectedImage;
private Uri imageUri;
private void activeTakePhoto() {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
selectedImage = data.getData();
imageView.setImageURI(selectedImage);
}
break;
case REQUEST_IMAGE_CAPTURE:
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
imageView.setImageURI(imageUri);
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
Log.e("A", "AAA");
}
}
}
submit.setOnClickListener(new View.OnClickListener() { // back to Activity A listView
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
amount = Amount.getText().toString();
description = Description.getText().toString();
type = spinnerType.getSelectedItem().toString();
returnIntent.putExtra("type", type);
returnIntent.putExtra("description", description);
returnIntent.putExtra("amount", amount);
if(selectedImage!=null) {
returnIntent.putExtra("img_uri", selectedImage.toString());
}
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
});
活动 A
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { // if listView is clicked
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) { // if list is clicked
mClickedPosition = position;
Object o = listview.getItemAtPosition(position);
ImageAndText image = (ImageAndText) o;
String type = image.getType();
String amount = image.getAmount();
String description = image.getDescription();
Uri photo = image.getImage();
String[] type1 = type.split(":");
String[] amount1 = amount.split(":");
String[] description1 = description.split(":");
Intent i = new Intent(getApplication(), AddMoreClaims.class);
i.putExtra("type", type1[1].toString().trim());
i.putExtra("amount", amount1[1].toString().trim());
i.putExtra("description", description1[1].toString().trim());
i.putExtra("photo", photo);
startActivityForResult(i, PROJECT_REQUEST_CODE);
}
});
编辑(添加时间戳)
private void activeTakePhoto() {
String filename = "Pic_" + System.currentTimeMillis() + ".jpg";
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo,filename));
imageUri = Uri.fromFile(photo,filename);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}