5

I try to delete a file using contentResolver but only delete the entry from database, not the real file. So I try delete first the entry and later the file:

int rows = context.getContentResolver().delete(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
MediaStore.Audio.Media._ID + "=" + idSong, null);

// Remove file from card
if (rows != 0) {
Uri uri = ContentUris.withAppendedId(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, idSong);
File f = new File(uri.getPath());
if(!f.delete())
    Log.d("fail-2", "fail-2");  
}
else
Log.d("fail-1", "fail-1");

...and the output is "fail-2". Why?

Why ContentResolver doesn't delete the real file? Is this normal?

4

3 回答 3

1

这是有效的:

    // Remove entry from database
    int rows = context.getContentResolver().delete(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            MediaStore.Audio.Media._ID + "=" + idSong, null);

    // Remove file from card
    if (rows != 0) {
        try {
            File f = new File(path);
            if (f.delete())
                return true;
        } catch (Exception e) {
            Log.d("MusicDB", "file: '" + path
                    + "' couldn't be deleted", e);
            return false;
        }
    }
    return false;

但是为什么 contentResolver 不删除文件?

于 2013-01-15T11:23:47.497 回答
0

似乎在 4.2 中,它会将文件归零,但不会删除它。我实际上希望它只是从 MediaStore 中删除它而不是从文件系统中删除它。无论哪种方式,这似乎是一个 Android 错误。

更新文件时遇到问题。我遇到的问题是媒体扫描仪不会在重新扫描时删除旧条目,因此您最终会得到两个条目。

于 2013-05-26T15:25:55.680 回答
0

在 kotlin 中试试这个

   contentResolver.delete(
          MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
          ,"_data" + "=?", 
          arrayOf(path) )
于 2020-10-02T14:50:42.030 回答