简体   繁体   中英

C# Xamarin.Android images are displayed in the file manager and not in the gallery

I made an application to save photos on the phone.

public void DownloadImage(string linkbitmap, string title)
    {           
        Bitmap bitmap = GetImageBitmapFromUrl(linkbitmap).Result;
        var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures + "/Tsundere");
        var complete = path.AbsolutePath;           
        var filepatch = System.IO.Path.Combine(complete, title + ".jpg");
        var filestream = new FileStream(filepatch, FileMode.Create);
        bitmap.Compress(Bitmap.CompressFormat.Png, 100, filestream);
        filestream.Close();
    }

The pictures are displayed in the file manager, but in the example the discord application does not have them. I have to restart the phone to be visible. How to make photos immediately visible in other applications, such as photos from the camera or photos downloaded from facebook?

Construct an Intent action ( Intent.ActionMediaScannerScanFile ) with a uri that includes the file path:

using (var file = new Java.IO.File(path))
using (var uri = Android.Net.Uri.FromFile(file))
{
    var scanFileIntent = new Intent(Intent.ActionMediaScannerScanFile, uri);
    SendBroadcast(scanFileIntent);
}

FYI: You can create/register a BroadcastReceiver that listens for Intent.ActionMediaScannerFinished to determine when the media scanner is done processing your request.

I solve this problem. Just add MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] {path}, null, null); in the code description this class https://developer.xamarin.com/api/type/Android.Media.MediaScannerConnection/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM