Android 文件删除在图库中留下空占位符

发布于 2024-10-07 16:57:00 字数 484 浏览 8 评论 0原文

我通过以下方式插入图像:

    ContentValues values = new ContentValues();   
    values.put(Images.Media.TITLE, filename);
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri uri = this.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

但是当我尝试删除时,

    File f = new File(imageURI);
    f.delete();

图片不再存在,但有一个空占位符。有什么想法吗?

I insert an image via:

    ContentValues values = new ContentValues();   
    values.put(Images.Media.TITLE, filename);
    values.put(Images.Media.DATE_ADDED, System.currentTimeMillis()); 
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri uri = this.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);

But when I try to delete

    File f = new File(imageURI);
    f.delete();

The picture is no longer there but an empty placeholder is. Any ideas?

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

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

发布评论

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

评论(9

陌伤浅笑 2024-10-14 16:57:01
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  MediaScannerConnection.scanFile(context,new String[]{selectedVideoDelete}, null, null);
} else {
  context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  MediaScannerConnection.scanFile(context,new String[]{selectedVideoDelete}, null, null);
} else {
  context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
}
画▽骨i 2024-10-14 16:57:00

Android 有某种缓存来跟踪媒体文件。

试试这个:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

它使 MediaScanner 服务再次运行,这应该会从设备的缓存中删除已删除的图像。

看起来您还需要将此权限添加到 AndroidManifest.xml 中:

<intent-filter>
  <action android:name="android.intent.action.MEDIA_MOUNTED" />
  <data android:scheme="file" /> 
</intent-filter>

Android has a cache of sorts that keeps track of media files.

Try this:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

It makes the MediaScanner service run again, which should remove the deleted image from the device's cache.

Looks like you also need to add this permission to your AndroidManifest.xml:

<intent-filter>
  <action android:name="android.intent.action.MEDIA_MOUNTED" />
  <data android:scheme="file" /> 
</intent-filter>
清醇 2024-10-14 16:57:00

正如评论中所述,接受的答案使用了大量内存。虽然 MediaScannerConnection 没有“deleteFile”方法,但删除文件后只需将旧文件路径传递给“scanFile”方法即可。介质扫描仪将重新扫描并移除介质。

在N5上测试过。安卓4.4。

编辑:其他人指出这在 4.2 上不起作用

new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {
            String filePath = recording.file.getAbsolutePath();
            recording.file.delete();
            MediaScannerConnection.scanFile(context,
                    new String[]{filePath}, null, null);
            return null;
        }
    }.execute();

As stated in the comments, the accepted answer uses a lot of memory. While MediaScannerConnection doesn't have a "deleteFile" method, just pass in the the old file path to the "scanFile" method once you've deleted the file. The media scanner will rescan and remove the media.

tested on N5. Android 4.4.

EDIT: Other have stated this doesn't work on 4.2

new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... params) {
            String filePath = recording.file.getAbsolutePath();
            recording.file.delete();
            MediaScannerConnection.scanFile(context,
                    new String[]{filePath}, null, null);
            return null;
        }
    }.execute();
清风挽心 2024-10-14 16:57:00

您还可以使用以下方法从图库中删除图像:

getContentResolver().delete(imageUri, null, null);

只需确保“imageUri”是您调用 insert 时返回给您的 uri。

You can also delete the image from the Gallery by using:

getContentResolver().delete(imageUri, null, null);

Just make sure that 'imageUri' is the uri returned to you when you called insert.

吃兔兔 2024-10-14 16:57:00

如果您不知道 @manisha 的答案中所需的 image-id 和
@Muhammad Waqas Khan 您还可以按文件名删除

    String fullPathToFile = "/storage/sdcard0/DICM/test.jpg";
    getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        MediaStore.Images.Media.DATA + "=?", new String[]{ fullPathToFile } );

[更新2016-06-16]

危险:这看起来像“仅从mediaDB中删除项目”。
由于 mediaDB 中有一个 SQLite-cascade-file-delete-trigger,因此引用的文件也会被删除(如果存在)。

为了避免这种情况,您必须在调用删除之前将 MediaStore.Images.Media.DATA 列更改为不存在的文件路径。

If you donot know the image-id as required in the answer of @manisha and
@Muhammad Waqas Khan you can also delete by file name

    String fullPathToFile = "/storage/sdcard0/DICM/test.jpg";
    getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        MediaStore.Images.Media.DATA + "=?", new String[]{ fullPathToFile } );

[Update 2016-06-16]

Danger: this looks like "Only delete the item from the mediaDB".
Since there is a SQLite-cascade-file-delete-trigger in the mediaDB the referenced file is also deleted if it exist.

To avoid this you have to change the column MediaStore.Images.Media.DATA to a non-existing filepath before calling delete.

╭ゆ眷念 2024-10-14 16:57:00

取自: http:// developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html

这对我有用(在 HTC Desire、Android 2.3 上测试)

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});

taken from: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.html

This worked for me ( tested on HTC Desire, Android 2.3)

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
∞梦里开花 2024-10-14 16:57:00

使用这个方法
getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );

Use this method
getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media._ID + "=?", new String[]{ Long.toString(id) } );

╰沐子 2024-10-14 16:57:00

File.delete(); 之后添加此行:

MediaScannerConnection.ScanFile(this,new string[]{fileUrl.ToString()}, null, null);

Add this line after File.delete();:

MediaScannerConnection.ScanFile(this,new string[]{fileUrl.ToString()}, null, null);
晚风撩人 2024-10-14 16:57:00

删除文件后立即添加:

MediaScannerConnection.scanFile(context, arrayOf(imageURI.getPath()), null, null)

:)

Add this right after deleting a file:

MediaScannerConnection.scanFile(context, arrayOf(imageURI.getPath()), null, null)

:)

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