I have been using Camera Intent to capture images. For the first time it works perfectly and I am able to capture image and show that in ImageView. But when I try second time to capture a different image it still shows the same old image.
Here is my code for Camera Intent and 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();
}
});
Activity 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);
}
});
Edited(add TimeStamp)
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);
}
If we take a look at the source for ImageView
, the setImageURI()
method starts out with an if
check similar to:
public void setImageURI(@Nullable Uri uri) {
if (mResource != 0 ||
(mUri != uri &&
(uri == null || mUri == null || !uri.equals(mUri)))) {
// Drawable resolution and update code
}
}
As you can see from !uri.equals(mUri)
, the ImageView
won't refresh itself if the Uri
passed into the method equals()
that already set. Your code is saving to the same file each time, so the Uri
s are always going to be the same.
Simply call imageView.setImageURI(null);
before calling imageView.setImageURI(imageUri);
. Alternatively, you could save to a different file each time; for example, by adding a timestamp to the filename.