我刚刚在Android应用程序中编写了一个函数,该函数使用Java中的标准"File"类删除文件.即:
String fileName= "/mnt/Gallery/Img001.jpg";
File file = new File(fileName);
file.delete();
Run Code Online (Sandbox Code Playgroud)
虽然上面的过程很简单,但我一直想知道通过'ContentResolver'做同样的事情是否有任何好处.任何意见,将不胜感激.
干杯,
Jarryd
------------------------------------------编辑------- ---------------------------------
以下是通过内容解析程序删除文件的示例.此示例假定要删除的文件是图像,并且其"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+ " ^_^");
}
Run Code Online (Sandbox Code Playgroud)