1

I want to add GPS information to an image. I created that image at a Uri returned by ContentResolver.Insert(). I found out that ExifInterface can be used to write the GPS information, but to use it I need the path to the image.

My question is - How do I get the pathToSavedImage (in the below code)?

Note: This is Xamarin C# code, but Android answers are very relevant.

var cr = Application.Context.ContentResolver;
Android.Net.Uri uri = cr.Insert(MediaStore.Images.Media.ExternalContentUri, values);

Stream outputStream = cr.OpenOutputStream(uri, "w");
image.CopyTo(outputStream);
outputStream.Close();

ExifInterface exifInterface = new ExifInterface(pathToSavedImage);
exifInterface.SetAttribute(key, value);
exifInterface.SaveAttributes();

EDIT:

What's present in values?

var values = new ContentValues();
values.Put(MediaStore.Images.Media.InterfaceConsts.Title, title);
values.Put(MediaStore.Images.Media.InterfaceConsts.DisplayName, name);
values.Put(MediaStore.Images.Media.InterfaceConsts.MimeType, "image/jpeg");
values.Put(MediaStore.Images.Media.InterfaceConsts.Description, description);
values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis());

var album = Environment.DirectoryPictures + Java.IO.File.Separator + albumName;

values.Put(MediaStore.Images.Media.InterfaceConsts.RelativePath, album);
values.Put(MediaStore.Images.Media.InterfaceConsts.IsPending, true);
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
  • Show what you put as `values`. With them you determined the path so you already know. – blackapps Dec 02 '22 at 10:41
  • @blackapps added the content of values above. – Shubham Agarwal Dec 02 '22 at 10:46
  • Why are you calling that album? So it lands in the public Pictures directory with file name 'albumName'. So then you will know full path as you determined it. Well not completely as relative path should be only directory. Put the albumName in DISPLAY_NAME column. – blackapps Dec 02 '22 at 10:50
  • Yes. The value of album is "Pictures/MyApp". The image does end up there. But it's a relative path, and exifInterface isn't able to resolve it. I want to get the full path so that exifInterface can find it. – Shubham Agarwal Dec 02 '22 at 10:52
  • Are you telling now that you dont know full path of public Pictures directory? – blackapps Dec 02 '22 at 10:53
  • Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_,PICTURES). – blackapps Dec 02 '22 at 10:54
  • @blackapps Using the path as mentioned in your comment did solve the path issue. I've access error now, which is a different problem. Thanks. – Shubham Agarwal Dec 02 '22 at 11:14
  • Which error exactly? Use File.exists() and File.canRead() and File.canWrite() on that full path before you let the exifinterface on it. – blackapps Dec 02 '22 at 11:21

0 Answers0