2013-01-15 164 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错误。

更新文件时遇到问题。我遇到的问题是媒体扫描器不会在重新扫描时删除旧条目,所以最终会出现两个条目。

相关问题