0

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

1 Answers1

0

First of all, please do not use contentresolver to delete the Image. For the reason, you can refer to this thread.

https://stackoverflow.com/a/45406158/10627299

Please try to use following method to check permission again. Before you want to delete the file in Api 23 or later.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using Android;
 using Android.App;
 using Android.Content;
 using Android.Content.PM;
 using Android.OS;
 using Android.Runtime;
 using Android.Support.V4.App;
 using Android.Views;
 using Android.Widget;

 namespace DeleteFile.Droid
{
  class PermisionUtils
  {
    // Storage Permissions
    private static  int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
        Manifest.Permission.ReadExternalStorage,
         Manifest.Permission.WriteExternalStorage};

    /**
     * Checks if the app has permission to write to device storage
     * If the app does not has permission then the user will be prompted to
     * grant permissions
     *
     * @param activity
     */
    public static void verifyStoragePermissions(Activity activity)
    {
        // Check if we have write permission
        Permission permission = ActivityCompat.CheckSelfPermission(activity,
                Manifest.Permission.WriteExternalStorage);

        if (permission != Android.Content.PM.Permission.Granted)
        {
            // We don't have permission so prompt the user
            ActivityCompat.RequestPermissions(activity, PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE);
        }
    }
   }
 }

Any of the standard C# APIs can be used to delete a file from external storage, such as System.IO.File.Delete

System.IO.File.Delete("/storage/emulated/0/Android/data/com.companyname.app/files/count.txt");

Leon
  • 8,404
  • 2
  • 9
  • 52