gpt4 book ai didi

android - 第二次从相机捕获时无法加载图像

转载 作者:行者123 更新时间:2023-11-29 00:04:39 30 4
gpt4 key购买 nike

我一直在使用 Camera Intent 捕捉图像。这是第一次它完美地工作,我能够捕获图像并在 ImageView 中显示它。但是当我第二次尝试拍摄不同的图像时,它仍然显示相同的旧图像。

这是我的 Camera Intent 和 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);
}
});

已编辑(添加时间戳)

 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);
}

最佳答案

如果我们看一下 ImageView 的源代码,setImageURI() 方法以 if 检查开始,类似于:

public void setImageURI(@Nullable Uri uri) {
if (mResource != 0 ||
(mUri != uri &&
(uri == null || mUri == null || !uri.equals(mUri)))) {

// Drawable resolution and update code
}
}

正如您从 !uri.equals(mUri) 中看到的,如果 Uri 传递到方法 equals() 已经设置。您的代码每次都保存到同一个文件,因此 Uri 总是相同的。

只需在调用 imageView.setImageURI(imageUri); 之前调用 imageView.setImageURI(null);。或者,您可以每次保存到不同的文件;例如,通过向文件名添加时间戳。

关于android - 第二次从相机捕获时无法加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34772580/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com