我刚刚在 Android 应用程序中使用 Java 的标准 'File' 类编写了一个删除文件的函数。例如:
String fileName= "/mnt/Gallery/Img001.jpg";
File file = new File(fileName);
file.delete();
虽然上述过程足够简单,但我一直在想是否通过“ContentResolver”执行相同操作有任何优势。如有建议,请指教。
------------------------------------------ 编辑 ----------------------------------------
以下是通过Content Resolver删除文件的示例。此示例假定要删除的文件是图像,其“id”已知。
long mediaId = 155; // NOTE: You would normally obtain this from the content provider!
Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri itemUri = ContentUris.withAppendedId(contentUri, mediaId);
int rows = getContentResolver().delete(itemUri, null, null);
String path = itemUri.getEncodedPath();
if(rows == 0)
{
Log.e("Example Code:","Could not delete "+path+" :(");
}
else
{
Log.d("Example Code:","Deleted "+path+ " ^_^");
}