2017-03-16 2 views
0

後でPicasaで読み込まれた画像を保存しています。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

TEMP_FILE_TITLE = "共有イメージ" を置き換えるあなたの場合は削除

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

のためにこれを試してみてください

関連する問題