What's the proper way to insert a row into ContentResolver

1.5k Views Asked by At

This is my logic to insert a new row into ContentResolver (it is Xamarin, but don't think it is relevant except for some syntax may looks weird for non C# guys)

var contentResolver = ContentResolver;
var cv = new ContentValues();
String timeStamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
cv.Put(MediaStore.Images.Media.InterfaceConsts.Title, timeStamp);
var uri = contentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, cv);

I was expecting uri to reflect the title that I have set but instead it returns a uri like this one

content://media/external/images/media/1546

The title I set is not present anywhere. What's the uri I'm getting from Insert and what's the proper way to make it honor the name I've set for the image?

EDIT

If I get MediaStore.Images.ImageColumns.Data, result is storage/emulated/0/Pictures/1483385168489.jpg

If I get MediaStore.Images.ImageColumns.Title, result is 20170102_165627

I expect to create a file with this name storage/emulated/0/Pictures/20170102_165627.jpg

1

There are 1 best solutions below

9
On BEST ANSWER

what's the proper way to make it honor the name I've set for the image?

Query ContentProvider using URI for MediaColumns.TITLE:

 // required column 
string[] proj = new[] { MediaStore.MediaColumns.TITLE };
 // query ContentProvider
ICursor cursor = contentResolver.Query(uri, proj, null, null, null);

// add null and empty check for cursor
 ....

// get column index
int column = cursor.GetColumnIndex(MediaStore.MediaColumns.TITLE);
// read title from cursor using column index
string media_title = cursor.GetString(column);