如何从SD卡删除照片?

时间:2018-10-26 15:11:49

标签: java android android-contentresolver mediastore

我在堆栈溢出中发现了该线程。我尝试了first answer。没用在delete()返回true之后,照片仍在手机的“照片”应用程序中。我尝试了this second answer。也没用。

然后我尝试了this third answer

这是我对第三个答案进行一些修改后的代码。 imagesEncodedList是我要删除的照片的字符串文件路径的ArrayList:

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();

我不知道contentResolver.query()是如何工作的。我运行了我的应用程序,并显示错误消息,指的是游标c初始化的行:

  

原因:java.lang.IllegalArgumentException:无法绑定索引2的参数,因为索引超出范围。该语句有1个参数。

哪个参数位于索引2?什么只有1个参数?

0 个答案:

没有答案
相关问题