如何在MediaStore中更新()图像

时间:2015-05-04 13:01:39

标签: android mediastore

query MediaStore,我只需使用正确的“基本URI”创建一个URI并附加IMAGE_ID(我从相应的缩略图中添加)。然后,我只是调用内容解析器来获取游标:

    Long IMAGE_ID = 172712; // let's say we have this IMAGE_ID
    Uri baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    baseUri = Uri.withAppendedPath(baseUri, ""+ IMAGE_ID);

    // let's just get this one field, description
    String[] projection = {MediaStore.Images.Media.DESCRIPTION};

    Cursor c = getContext().getContentResolver().query(baseUri, projection, null, null, null);
    if (!c.moveToFirst()) return -1; 
    String description = c.getString(0);  // and we're done :-)

但是,如果我想设置 MediaStore.Images.Media.DESCRIPTION字段怎么办?我可以以某种方式调用update方法吗?

    Long IMAGE_ID = 172712; // same image...
    Uri baseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    baseUri = Uri.withAppendedPath(baseUri, ""+ IMAGE_ID);

    // create an object with the value to set
    ContentValues test_values = new ContentValues();
    test_values.put(MediaStore.Images.Media.DESCRIPTION, "Some image text...");

    // i.e., SQL like: UPDATE MEDIASTORE SET DESCRIPTION = "Some image text..." WHERE IMAGE_ID = 172712;
    String sIMAGE_ID = "" + IMAGE_ID;
    int res = getContext().getContentResolver().update(baseUri, test_values, MediaStore.Images.Media._ID + "= ?", new String[]{sIMAGE_ID });  // works fine!

1 个答案:

答案 0 :(得分:0)

上面的代码工作正常(至少在模拟器中,Nexus 7设备)! update设置DESCRIPTION“字段”。