1

I found this thread on Stack Overflow. I tried the first answer. It didn't work. The photos are still there in the phone's Photos app after delete() returns true. I tried this second answer. Didn't work either.

Then I tried this third answer.

Here is my code after some adaptation of the third answer. imagesEncodedList is an ArrayList of String file paths of the photos I want to delete:

String[] selectionArgs = new String[imagesEncodedList.size()];
for(int i=0; i<imagesEncodedList.size();i++) {
  selectionArgs[i]=imagesEncodedList.get(i);
 }

 //delete original
// Set up the projection (we only need the ID)
 String[] projection = { MediaStore.Images.Media._ID };

// Match on the file path
 String selection = MediaStore.Images.Media.DATA + " = ?";

// Query for the ID of the media matching the file path
 Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
 ContentResolver contentResolver = getContentResolver();
Cursor c = contentResolver.query(queryUri, projection, selection, 
  selectionArgs, null);
if (c.moveToFirst()) {
   // We found the ID. Deleting the item via the content provider will also 
    remove the file
   long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
  Uri deleteUri = 
  ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
contentResolver.delete(deleteUri, null, null);
  } else {
    // File not found in media store DB
  }
 c.close();

I don't known how contentResolver.query() work. I ran my app and the error says, referring to the line where Cursor c was initialized:

Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 1 parameters.

Which argument is at index 2? What has only 1 parameter?

joe doe
  • 117
  • 9

0 Answers0