使用Android相机时如何删除图库中的重复图片?

发布于 2024-10-28 07:13:48 字数 403 浏览 9 评论 0原文

我在 Activity 中使用 Android 相机拍照:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImagePath());
startActivityForResult(intent, TAKE_PHOTO_CODE);

当我离开相机应用程序时,照片保存在两个位置:

  1. getImagePath() 方法指定的路径(这是正确的);
  2. 进入画廊。我不想要那个。

如何防止照片被保存到图库中?如果我做不到,如何在 onActivityResult() 中获取照片路径以便将其删除?

I use the Android camera to take a picture in my activity :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImagePath());
startActivityForResult(intent, TAKE_PHOTO_CODE);

When I leave the camera app, the photo is saved in two places:

  1. At the path specified by the getImagePath() method (which is correct) ;
  2. Into the gallery. I don't whant that.

How can I prevent the photo to be saved into the gallery? And if I can't do it, how can I get the photo path in my onActivityResult() so I can delete it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-11-04 07:13:48

对于 Intent.ACTION_GET_CONTENT,您可以删除图库文件(将其复制到文件夹后)。也许这也适用于 MediaStore.ACTION_IMAGE_CAPTURE (使用 MediaStore.EXTRA_OUTPUT)。我使用以下代码片段在从 Intent.ACTION_GET_CONTENT 返回时删除图库文件:

public String getRealPathFromURI(Uri contentUri) {
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = managedQuery(contentUri, proj, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  Uri u = data.getData();
  new File(getRealPathFromURI(u)).delete();
  ...

For Intent.ACTION_GET_CONTENT you can delete the gallery file (after copying it to your folder). Perhaps this would also work for MediaStore.ACTION_IMAGE_CAPTURE (with MediaStore.EXTRA_OUTPUT). I use the following code snippet for deleting the gallery file on return from Intent.ACTION_GET_CONTENT:

public String getRealPathFromURI(Uri contentUri) {
  String[] proj = { MediaStore.Images.Media.DATA };
  Cursor cursor = managedQuery(contentUri, proj, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  Uri u = data.getData();
  new File(getRealPathFromURI(u)).delete();
  ...
笔落惊风雨 2024-11-04 07:13:48

您是否在 getImagePath() 代码中将图像保存到 SD 卡中?如果是这样,Android 媒体扫描程序将从该位置拾取它以显示在图库中。您可以在保存图像的目录中创建一个名为 .nomedia 的空文件,以使媒体扫描程序忽略该文件夹中的所有媒体文件。

Are you saving the image to the SD card somewhere in your getImagePath() code? If so, the Android Media Scanner is picking it up from that location to display in the gallery. You can create an empty file named .nomedia in the directory where your images are saved to have the Media Scanner ignore all the media files in that folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文