无法删除使用MediaStore插入的图像 [英] Cannot remove an image inserted with MediaStore

查看:176
本文介绍了无法删除使用MediaStore插入的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在保存一张载有Picasso的图片,以便以后共享。这是我用来保存它的代码:

I'm saving an image loaded with Picasso to be shared afterwards. This is the code I'm using to save it:

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

return Uri.parse(path);

现在,当共享图像时,我必须使用URI删除它。我已经尝试了一些我在这里看到的答案,但没有一个有效。图像仍显示在图库中。这些是我使用的方法:

Now, when the image has been shared, I have to delete it using the URI. I've tried some answers I've seen here but none of them has worked. The image still appears in the gallery. These are the methods I have used:

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

我知道我做错了什么?

谢谢,

推荐答案

尝试将其删除

    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 =共享图像

In your case replace TEMP_FILE_TITLE = "Shared image"

这篇关于无法删除使用MediaStore插入的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆