Javaの標準の「File」クラスを使用してファイルを削除する関数をAndroidアプリに記述しました。すなわち:
String fileName= "/mnt/Gallery/Img001.jpg";
File file = new File(fileName);
file.delete();
上記の手順は非常に簡単ですが、「ContentResolver」を介して同じことを行うことに利点があるかどうか疑問に思っています。何かアドバイスをいただければ幸いです。
- - - - - - - - - - - - - - - - - - - - - 編集 - - - - ---------------------------------
これは、コンテンツリゾルバーを介してファイルを削除する例です。この例では、削除されるファイルがイメージであり、その「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+ " ^_^");
}