如何在 MediaStore 中更新()图像
How to update() an image in MediaStore
到 query
MediaStore,我只需创建一个具有正确 "base 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!
以上代码运行良好(至少在模拟器、Nexus 7 设备上)! update
设置描述 "field".
到 query
MediaStore,我只需创建一个具有正确 "base 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!
以上代码运行良好(至少在模拟器、Nexus 7 设备上)! update
设置描述 "field".