Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete a file from contentResolver only delete the entry from database (not file)

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?

like image 974
jesuslinares Avatar asked Nov 12 '22 13:11

jesuslinares


1 Answers

This is working:

    // 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;

But why contentResolver doesn't delete the file??

like image 77
jesuslinares Avatar answered Nov 15 '22 04:11

jesuslinares