I am trying to delete images from sd card which are saved through mobile applicaton.
I am using contentresolver.Delete in xamarin forms By which it deletes image temporarily.
In sd card image details path is different and in File manager images for sd card are saving in different path .
If I use
File.delete(file)
It throws an exception saying i dont have permission to delete file in that location ( which is sd card image path)
Below are permissions added in Android manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS" />
Code to find sd card path :
var file1 = new Java.IO.File("storage/");
listOfStorages = file1.ListFiles();
if (listOfStorages[1].Name.Contains("emulated") || listOfStorages[0].Name.Contains("-"))
{
isSDPresent = true;
var sdfilepath = listOfStorages[0].AbsolutePath + "/DCIM/Camera/";
bool status = Android.OS.Environment.IsExternalStorageEmulated;
List<string> sdimagelist = Directory.GetFiles(sdfilepath).Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date).ToList();
foreach (var filpath in sdimagelist)
{
if (!currentDCIMImagesList.Contains(filpath))
{
currentDCIMImagesList.Add(filpath);
}
}
}
Code to delete image from Sd card :
var query = afterDCIMImagesList.Where(item => !currentDCIMImagesList.Contains(item));
foreach (string file in query)
{
Java.IO.File aFile = new Java.IO.File(file);
string where = MediaStore.MediaColumns.Data + "=?";
string[] selectionArgs = new string[] { aFile.AbsolutePath };
ContentResolver contentResolver = context.ContentResolver;
Android.Net.Uri filesUri = MediaStore.Files.GetContentUri("external");
if (aFile.Exists())
{
//Android.Net.Uri uri = Android.Net.Uri.Parse(file);
//contentResolver.Delete(uri, null, null);
contentResolver.Delete(filesUri, where, selectionArgs);
}
Please help me resolving this issue. Thanks