【问题标题】:When download image to pictures directory it isn't being shown on gallery [Xamarin Forms]将图像下载到图片目录时,它不会显示在图库 [Xamarin Forms] 上
【发布时间】:2018-02-05 03:23:46
【问题描述】:

我正在使用下面的代码下载图片:

var webClient = new WebClient();
webClient.DownloadDataCompleted += (s, e) =>
{
    var bytes = e.Result; // get the downloaded data
                          //string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    string documentsPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/" + Android.OS.Environment.DirectoryPictures + "/";
    string localFilename = img.Uri.LocalPath.Split('/').Last();
    string localPath = System.IO.Path.Combine(documentsPath, localFilename);
    File.WriteAllBytes(localPath, bytes); // writes to local storage
    Bitmap bmp = BitmapFactory.DecodeFile(localPath);

    using (var os = new FileStream(localPath, FileMode.Truncate))
    {
        bmp.Compress(Bitmap.CompressFormat.Jpeg, 95, os);
    }

};
webClient.DownloadDataAsync(img.Uri);

问题是图像显示在文件上而不是画廊上。我在安卓上运行。

我该如何解决这个问题?

【问题讨论】:

标签: android xamarin download xamarin.forms gallery


【解决方案1】:

您可以通过以下方式手动触发 MediaStore 更新:

var pathToPublicImage = "/storage/emulated/0/Download/stackoverflow.png";

if (Build.VERSION.SdkInt >= BuildVersionCodes.Kitkat)
{
    MediaScannerConnection.ScanFile(this, new string[] { pathToPublicImage }, null, this);
}
else
{
    using (var file = new Java.IO.File(pathToPublicImage))
    using (var uri = Uri.FromFile(file))
    {
        SendBroadcast(new Intent(Intent.ActionMediaScannerScanFile, uri));
    }
}

MediaScannerConnection.IONScanCompletedListener

public void OnScanCompleted(string path, Uri uri)
{
    Log.Debug("SO", "Media Scan Finished, Open Gallery Now");
}

Intent.ActionMediaScannerScanFile开始/结束

[BroadcastReceiver(Exported = true)]
[IntentFilter(new string[] { Intent.ActionMediaScannerStarted, Intent.ActionMediaScannerFinished })]
public class MediaUpdates : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        switch (intent.Action)
        {
            case Intent.ActionMediaScannerStarted:
                Log.Debug("SO", "Media Scan Started");
                break;
            case Intent.ActionMediaScannerFinished:
                Log.Debug("SO", "Media Scan Finished, Open Gallery Now");
                break;
        }
    }
}

【讨论】:

  • @GiorgeCaique 在您的Xamarin.Android 应用程序项目中。
  • 正在显示错误“SendBroadcast 在当前上下文中不存在”。
  • SendBroadcast : 使用 Activity 或 Application 级别的上下文
  • 抱歉,我是 Xamarin Forms 开发的新手。我在 PCL 项目上使用接口,并且在 iOS 和 Android 项目中实现。我仍然可以使用与依赖项一起工作的活动吗?您能否发布此活动代码的示例?
【解决方案2】:

我建立了一个解决方案。

就用这个方法:

private void UpdateGallery()
{
    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
    Java.IO.File file = new Java.IO.File(_path);
    Android.Net.Uri contentUri = Android.Net.Uri.FromFile(file);
    mediaScanIntent.SetData(contentUri);
    Application.Context.SendBroadcast(mediaScanIntent);
} 

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2018-09-16
    • 2016-06-15
    • 2019-10-25
    • 2014-02-27
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多