DespeiL
DespeiL

Reputation: 1033

Xamarin.Forms move image to gallery in Android 12 and above

I know that is easy to take a photo and save it to Gallery.

protected async Task<MediaFile> TakePhoto()
{
    var storageOptions = new StoreCameraMediaOptions()
    {
        SaveToAlbum = true,
        Directory = pictureAlbumName,
        Name = $"test_{DateTime.Now.ToString("HH_mm_ss_ff")}.jpg"
    };

    return await CrossMedia.Current.TakePhotoAsync(storageOptions);
 
}

As the result I got the URL that looks like this:

/storage/emulated/0/Android/data/com.companyname.appname/files/Pictures/MyAlbum/photo_18_47_29_69.jpg

But when I tried to save the image from bytes it appears in the folder but never appears in the gallery. After saving the image I tried of course to scan the newly created path but there was no effect

First attempt

File.WriteAllBytes("/storage/emulated/0/Android/data/com.companyname.appname/files/Pictures/MyAlbum/downloaded_image_223213a3as.jpg", imageBytes);
MediaScannerConnection.ScanFile(Application.Context, new string[] { path },null,null);

Second attempt using obsoleted Android methods

    Java.IO.File storagePath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
    string path = System.IO.Path.Combine(storagePath.ToString(), filename);
    System.IO.File.WriteAllBytes(path, imageByte);
    var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
    mediaScanIntent.SetData(Android.Net.Uri.FromFile(new Java.IO.File(path)));
    CurrentContext.SendBroadcast(mediaScanIntent);

Upvotes: 1

Views: 564

Answers (1)

FreakyAli
FreakyAli

Reputation: 16409

Update:

Basically you need to use this method and save it

private void SaveImageToStorage(Bitmap bitmap)
        {
            Stream imageOutStream;

            if (Build.VERSION.SdkInt >= BuildVersionCodes.Q)
            {

                ContentValues values = new ContentValues();
                values.Put(MediaStore.IMediaColumns.DisplayName,
                "image_screenshot.jpg");
                values.Put(MediaStore.IMediaColumns.MimeType, "image/jpeg");
                values.Put(MediaStore.IMediaColumns.RelativePath,
                Android.OS.Environment.DirectoryPictures + Java.IO.File.PathSeparator + "AppName");

                Android.Net.Uri uri = this.ContentResolver.Insert(MediaStore.Images.Media.ExternalContentUri, values);

                imageOutStream = ContentResolver.OpenOutputStream(uri);

            }
            else
            {

                String imagesDir =Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString() + "/AppName";
                imageOutStream = File.OpenRead(System.IO.Path.Combine(imagesDir, "image_screenshot.jpg"));
            }


            bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, imageOutStream);
            imageOutStream.Close();

        }

OG Answer:

As far as I know, Only images in your media store provider are visible to your gallery and to add it to the media store you need to use the following:

MediaStore.Images.Media.InsertImage(Activity.ContentResolver, ImgBitmap, yourTitle , yourDescription);

Hope this helps :)

Upvotes: 1

Related Questions