2013-01-15 161 views
4

我嘗試使用contentResolver刪除文件,但只刪除數據庫中的條目,而不是真正的文件。所以我嘗試先刪除條目然後刪除文件:從contentResolver刪除文件只刪除數據庫中的條目(不是文件)

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

...並且輸出是「fail-2」。爲什麼?

爲什麼ContentResolver不會刪除真實文件?這是正常的嗎?

回答

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的不刪檔?

0

看起來在4.2中,它將文件置零,但不會將其刪除。我實際上希望它只是將其從MediaStore中刪除,而不是從文件系統中刪除它。無論哪種方式,這似乎是一個Android錯誤。

更新文件時遇到問題。我遇到的問題是媒體掃描器不會在重新掃描時刪除舊條目,所以最終會出現兩個條目。

相關問題