-2

I Know this is already answered question.But I'm unable to figure it out when using SQLite DB. My app captures some documents and will be stores in phone memory. I'm using SQLite DB in my app which stores the path of the above image. How can i delete the image from phone memory if i delete the image in SQLite DB.

String photoPath = cursor.getString(i_COL_PICTURE);

--My path is

`"content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F153/ORIGINAL/NONE/1743496576"

`

Akhila
  • 3,235
  • 1
  • 14
  • 30

3 Answers3

3

When you want delete some file in your storage, Just do this.

File file = new File(yourFilePathHere);
deleted = file.delete();

I am considering you have required permissions because you are able to write files in storage.

Edit

You are using MediaStore for getting images. So now when you want delete file you should delete file from MediaStore also. I have a method which will help you.

public static int deleteFileFromMediaStore(final ContentResolver contentResolver, final File file) {
    String canonicalPath;
    try {
        canonicalPath = file.getCanonicalPath();
    } catch (IOException e) {
        canonicalPath = file.getAbsolutePath();
    }
    final Uri uri = MediaStore.Files.getContentUri("external");
    final int result = contentResolver.delete(uri,
            MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
    if (result == 0) {
        final String absolutePath = file.getAbsolutePath();
        if (!absolutePath.equals(canonicalPath)) {
            int deletedRow = contentResolver.delete(uri,
                    MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
            return deletedRow;
        }
    } else return result;
    return result;
}

Call it in your Activity like

deleteFileFromMediaStore(getContentResolver(), fileToDelete)

Note Check if you are getting absolute path by MediaStore. Here is my method to get all gallery images if you have problem with your code.

  public static ArrayList<ModelBucket> getImageBuckets(Context context) {
        ArrayList<ModelBucket> list = new ArrayList<>();
        String absolutePathOfImage;
        String absoluteFolder;
        boolean same_folder = false;
        int pos = 0;
        Uri uri;
        Cursor cursor;
        int column_index_data, column_index_folder_name;

        uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
        final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
        cursor = context.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
        if (cursor == null) return null;
        column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);
            absoluteFolder = cursor.getString(column_index_folder_name);
            Log.d("Column", absolutePathOfImage);
            Log.d("Folder", absoluteFolder);

            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFolderName().equals(absoluteFolder)) {
                    same_folder = true;
                    pos = i;
                    break;
                } else {
                    same_folder = false;
                }
            }
            if (same_folder) {
                ArrayList<String> al_path = new ArrayList<>(list.get(pos).getAllFilesPath());
                al_path.add(absolutePathOfImage);
                list.get(pos).setAllFilesPath(al_path);
            } else {
                ArrayList<String> al_path = new ArrayList<>();
                al_path.add(absolutePathOfImage);
                ModelBucket modelBucket = new ModelBucket();
                modelBucket.setFolderName(absoluteFolder);
                modelBucket.setAllFilesPath(al_path);
                list.add(modelBucket);
            }
        }
        return list;
    }

here ModelBucket.class is a model class.

public class ModelBucket {
    String folderName;
    ArrayList<String> allFilesPath;
    ArrayList<ModelFile> files;

   // make getter setter 
    }
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • This is the correct way to delete file, now check your path by debugging, check if file on your path exist or not. and tell bit more what you mean by `its not working`, you can show me the error. – Khemraj Sharma Jul 24 '18 at 05:10
  • I have updated my path in the question, This method is not working for the path which i have updated. When we access the path which is stored in SQLite DB. Its gives me the above path and I'm unable to extract the actual path. Thank you :)\ – Akhila Jul 24 '18 at 13:00
  • You are getting files from media store, that will need different method to delete. – Khemraj Sharma Jul 24 '18 at 13:07
  • Make sure you are getting path by `cursor.getColumnIndex(MediaStore.MediaColumns.DATA)`, This will return you absolute path of file. – Khemraj Sharma Jul 24 '18 at 13:08
  • By the way file.delete method will not help you, because it delete file from directory not media store. – Khemraj Sharma Jul 24 '18 at 13:09
  • Thank you Khemraj :). I will check this out – Akhila Jul 24 '18 at 14:15
0

before deleting the image get the path of the image and pass the path to below code

File fdelete = new File(path);

if (fdelete.exists()) {
    if (fdelete.delete()) {
        System.out.println("file Deleted :" + path);
    } else {
        System.out.println("file not Deleted :" + path);
    }
}

after this remove the path from sqlite db

BASAVARAJ PATIL
  • 436
  • 3
  • 9
0

If you have your Uri pointing to the file you can do :

String pathToFile = myUri.getEncodedPath(); // this gives your the real path to the file, like /emulated/0/sdcard/myImageFile.jpg

File file = new File(pathToFile);

if(file.exists()){
   file.delete();
}
Samet
  • 917
  • 12
  • 26