【问题标题】:View a specific image from the complete gallery path without browsing无需浏览即可从完整图库路径查看特定图像
【发布时间】:2020-04-10 13:08:43
【问题描述】:

我正在尝试:

   <Image Source="/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg"/>

没有效果。
文件 imageSpc = new File('/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg');
var imageSpc = new File('/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg');
错误,无法为 File 或 var 类型创建静态变量。

            <Image x:Name="fotka" Aspect="AspectFit" HeightRequest = "50"
    WidthRequest = "60"/>

fotka.Source = ImageSource.FromFile("/storage/emulated/0/Pictures/Test/IMG_20200408_085036.jpg");
没有效果
可以在 XAML 中或至少从代码中显示特定图像

我确实在设备中启用了 ADB 通知选项。 在要显示的那一刻图像编译器显示错误:

如何解决这个错误?

【问题讨论】:

    标签: xaml xamarin


    【解决方案1】:

    根据您的描述,您希望使用图库路径显示图像,如果是,请查看以下代码:

    首先,您需要从 Android 或 ios 中的图库中获取图片路径。

    在 PCL(Forms) 中创建接口 IFileSystem

    public interface IFileSystem
    {       
         string GetGalleryImage();
    }
    

    然后在平台上实现这个接口,例子在Android中。

    [assembly: Dependency(typeof(FileSystemImplementation))]
    namespace demo3.Droid
    {
    public class FileSystemImplementation : IFileSystem
    {
    
        public string GetGalleryImage()
        {      
            var filePath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures);
            var path = System.IO.Path.Combine(filePath.AbsolutePath, "Test/image1.jpg");
            return path;
        }
    }
    }
    

    在PCL(Forms)中添加一个Image控件,命名为image1,使用DependencyService获取图片路径。

      <Button
                x:Name="btn1"
                Clicked="Btn1_Clicked"
                Text="load image" />
            <Image
                x:Name="image1"
                HeightRequest="100"
                WidthRequest="100" />
    
      private void Btn1_Clicked(object sender, EventArgs e)
        {         
            var path = DependencyService.Get<IFileSystem>().GetGalleryImage();
            image1.Source = ImageSource.FromFile(path);
        }
    

    这是一篇关于DependencyService的文章,你可以看看:

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

    更新:

    【讨论】:

    • 我试过了,但我的类环境不包含 GetExternalStoragePublicDirectory 和 DirectoryPictures 方法。我的环境仅包含与存储操作相关的 GetFolderPath 和 GetLogicalDrives。 Min Android 我已设置为使用 Pie 编译的 Lolipop。两种权限 R/W 均已启用。
    • @TamteeTa 我在 Android 项目中使用 Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures) 并没有任何问题,所以请在 github 上查看我的示例:@ 987654324@
    • 我测试了这段代码,没有任何错误或警告。设备上的应用程序仅显示按钮加载图像。点击按钮后不会发生任何事情。我认为这个问题更深刻。通过编译获得的非常详细的信息将对我有所帮助,但所有详细选项都已启用编译器,输出控制台上没有任何错误或警告,这可能是基本问题。按钮输入的图像路径是正确的,图像当然存在。
    • @Tamtee 第一次点击按钮时,您将获得读取设备文件的权限,然后您需要再次点击按钮才能从图库中加载图像。
    • 是的,权限已在应用程序首次运行时授予。 Kotlin 和 AS 中的类似应用程序正常工作。我是 Xamarin 新手,没有调试 Xamarin 项目的经验。
    【解决方案2】:

    我发现,在活动中显示之前的每个图像都必须调整为最大尺寸 4096 x 4096 像素,否则图像不会显示,没有任何错误消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 2015-09-29
      • 1970-01-01
      • 2023-01-28
      • 2013-04-10
      • 1970-01-01
      相关资源
      最近更新 更多