【问题标题】:Picture appears only after restart the device重启设备后才会出现图片
【发布时间】:2017-07-26 11:16:02
【问题描述】:

我有一个 Android 应用程序,它可以拍照并将它们保存在外部存储(“DCIM/相机”)中。但这些图片只有在我重新启动后才会出现。 是否有某种更新或解决方法?

我保存图像的源代码:

var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDcim);
var pictures = dir.AbsolutePath + "/Camera";
string name = System.DateTime.Now.ToString("yyyyMMdd_HHmmssfff") + ".jpg";
string filePath = System.IO.Path.Combine(pictures, name);
FileStream output;
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
try
{
    output = new FileStream(filePath, FileMode.Create);
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, output);
    output.Close();
    //Static Class that contains an methode for MediaScannerConnection.ScanFile 
    MediaGalleryHelper.AddFileToGallery(name); 
}
catch (System.Exception ex)
{
    System.Console.WriteLine(ex.ToString());
}

【问题讨论】:

    标签: c# android xamarin camera


    【解决方案1】:

    您必须让照片库知道已添加新照片。您可以使用以下代码:

    this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + filePath)));
    

    “this”是一项活动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-29
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多