2017-03-16 76 views
0

我正在保存加載畢加索的圖像以便後續共享。這是我用來保存它的代碼:無法刪除使用MediaStore插入的圖像

String path = MediaStore.Images.Media.insertImage(
        mContext.getContentResolver(), mImageBitmap, "Shared image", null); 

return Uri.parse(path); 

現在,當圖像已被共享時,我必須使用URI將其刪除。我嘗試了一些我在這裏看到的答案,但沒有一個能夠奏效。圖像仍然出現在畫廊中。這是我所使用的方法:


// 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 + " = ?"; 
String[] selectionArgs = new String[] { new File(mImageUri.toString()).getAbsolutePath() }; 

// 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); 
} 

c.close(); 

File file = new File(mImageUri.toString()); 
// This returns false 
file.delete(); 

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(mImageUri.toString())))); 

什麼,我做錯了任何想法?

感謝,

回答

1

試試這個刪除

String[] retCol = { MediaStore.Audio.Media._ID }; 

     Cursor cur = context.getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       retCol, 
       MediaStore.Images.Media.TITLE + "='"+TEMP_FILE_TITLE+"'", null, null 
     ); 
    try { 
     if (cur.getCount() == 0) { 
      return; 
     } 
     cur.moveToFirst(); 
     int id = cur.getInt(cur.getColumnIndex(MediaStore.MediaColumns._ID)); 
     Uri uri = ContentUris.withAppendedId(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id 
     ); 
     int cnt = context.getContentResolver().delete(uri, null, null); 
}finally { 
       cur.close(); 
      } 

在你的情況下更換TEMP_FILE_TITLE = 「共享圖像」