简体   繁体   English

C#Xamarin.Android图像显示在文件管理器中,而不显示在图库中

[英]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? 如何使照片在其他应用程序中立即可见,例如来自相机的照片或从Facebook下载的照片?

Construct an Intent action ( Intent.ActionMediaScannerScanFile ) with a uri that includes the file path: 使用包含文件路径的uri构造一个Intent操作( Intent.ActionMediaScannerScanFile ):

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. 仅供参考:您可以创建/注册一个侦听Intent.ActionMediaScannerFinished的BroadcastReceiver,以确定媒体扫描器何时完成处理您的请求。

I solve this problem. 我解决了这个问题。 Just add MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] {path}, null, null); 只需添加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/ 在代码说明中,此类https://developer.xamarin.com/api/type/Android.Media.MediaScannerConnection/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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