How to save image in gallery when APILevel>29?
aluzi liu
486
Reputation points
[Xamarin.Forms Android]
My app will download image from server and save to gallery, I try this:
Android.Net.Uri u = null;
if (Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Q)
{
ContentValues values = new ContentValues();
values.Put(MediaStore.IMediaColumns.MimeType, "image/jpeg");
values.Put(MediaStore.IMediaColumns.DateAdded, Java.Lang.JavaSystem.CurrentTimeMillis());
values.Put(MediaStore.IMediaColumns.DisplayName, filename);
var cr = MainActivity.AndroidContext.ContentResolver;
u = cr.Insert(MediaStore.Images.Media.ExternalContentUri, values);
var s = cr.OpenOutputStream(u);
s.Write(data, 0, data.Length);
s.Close();
values.Clear();
values.Put(MediaStore.IMediaColumns.IsPending, false);
int updated = cr.Update(u, values, null, null);
}
After running this code, I can confirm that image was save to:
/storage/emulated/0/Pictures/xxxxxxxxxxxx.jpg
But I can not see it in gallery, how to make it show in gallery?
Developer technologies .NET Xamarin
5,380 questions
Sign in to answer