Last active
September 14, 2020 10:42
-
-
Save MilenPavlov/98e210e3ffe1fbe4ecd9 to your computer and use it in GitHub Desktop.
ContentObserver for Xamarin Android Photos using MediaStore.IntentActionStillImageCamera
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//PhotoObserver.cs | |
public class Media | |
{ | |
public System.DateTime Added { get; set; } | |
public File File { get; set; } | |
public System.DateTime Modified { get; set; } | |
public string Type { get; set; } | |
public override string ToString() | |
{ | |
try | |
{ | |
return string.Format("{0} {1} Added: {2}, Modified {3}", File.AbsolutePath, Type, Added, Modified); | |
} | |
catch (System.Exception e) | |
{ | |
return "Error to-stringing the media object: " + e.Message; | |
} | |
} | |
} | |
public class PhotosObserver : ContentObserver | |
{ | |
private readonly Context _context; | |
private readonly string _name; | |
private readonly Uri _uri; | |
public PhotosObserver(Context context, string name, Uri uri) | |
: base(null) | |
{ | |
_context = context; | |
_uri = uri; | |
_name = name; | |
} | |
public event System.EventHandler DataUpdated = delegate { }; | |
public static PhotosObserver CreateExternalObserver(Context context) | |
{ | |
return new PhotosObserver(context, "External", MediaStore.Images.Media.ExternalContentUri); | |
} | |
public static PhotosObserver CreateInternalObserver(Context context) | |
{ | |
return new PhotosObserver(context, "Internal", MediaStore.Images.Media.InternalContentUri); | |
} | |
public override void OnChange(bool selfChange) | |
{ | |
base.OnChange(selfChange); | |
Media media = ReadFromMediaStore(_context, _uri); | |
if (media != null) | |
{ | |
string saved = string.Format("{0} detected {1}", _name, media); | |
} | |
//send the media info back | |
DataUpdated(media, System.EventArgs.Empty); | |
} | |
public PhotosObserver Register() | |
{ | |
var resolver = _context.ApplicationContext.ContentResolver; | |
resolver.RegisterContentObserver(_uri, true, this); | |
return this; | |
} | |
private static DateTime FromUnixTime(long unixTime) | |
{ | |
var epoch = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Local); | |
return epoch.AddSeconds(unixTime); | |
} | |
private Media ReadFromMediaStore(Context context, Uri uri) | |
{ | |
ICursor cursor = context.ContentResolver.Query(uri, null, null, null, "date_added DESC"); | |
Media media = null; | |
if (cursor.MoveToNext()) | |
{ | |
int dataColumn = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.Data); | |
int mimeTypeColumn = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.MimeType); | |
int modifiedColumn = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.DateModified); | |
int dateAddedColumn = cursor.GetColumnIndexOrThrow(MediaStore.Images.Media.InterfaceConsts.DateAdded); | |
string filePath = cursor.GetString(dataColumn); | |
string mimeType = cursor.GetString(mimeTypeColumn); | |
var added = cursor.GetLong(dateAddedColumn); | |
var modified = cursor.GetLong(dateAddedColumn); | |
media = new Media() | |
{ | |
File = new File(filePath), | |
Type = mimeType, | |
Modified = FromUnixTime(modified), | |
Added = FromUnixTime(added), | |
}; | |
} | |
cursor.Close(); | |
return media; | |
} | |
} | |
//in TakePhotos Activity / Fragment in my example: | |
private List<PhotosObserver> _observers; | |
//call this OnCreate() | |
private void RegisterObserver() | |
{ | |
_observers = new List<PhotosObserver>(){ | |
PhotosObserver.CreateExternalObserver(Activity).Register(), | |
}; | |
} | |
foreach (var photosObserver in _observers) | |
{ | |
photosObserver.DataUpdated += (sender, args) => | |
{ | |
var media = (Media) sender; | |
if (media != null) | |
{ | |
//media info is received here... | |
} | |
}; | |
} | |
Can you please give me a full example on the MainActivity on how to do this?
Thank You!!!
How to use this do you its complete eg?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example using MediaStore.IntentActionStillImageCamera intent together with ContentObserver checking the external storage directory for newly saved photos, so user can take many photos and then dismiss the camera Intent..