簡體   English   中英

C#Xamarin.Android圖像顯示在文件管理器中,而不顯示在圖庫中

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

我提出了一個將照片保存在手機上的應用程序。

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();
    }

這些照片顯示在文件管理器,但本例中的不和諧應用程序不具有它們。 我必須重新啟動手機才能看到它。 如何使照片在其他應用程序中立即可見,例如來自相機的照片或從Facebook下載的照片?

使用包含文件路徑的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);
}

僅供參考:您可以創建/注冊一個偵聽Intent.ActionMediaScannerFinished的BroadcastReceiver,以確定媒體掃描器何時完成處理您的請求。

我解決了這個問題。 只需添加MediaScannerConnection.ScanFile(Android.App.Application.Context, new string[] {path}, null, null); 在代碼說明中,此類https://developer.xamarin.com/api/type/Android.Media.MediaScannerConnection/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM