Ada*_*zek 2 c# xamarin xamarin.forms xamarin.essentials
我在 Android 上的本机应用程序中打开 JPG 文件时遇到问题。我正在使用最新版本的 Xamarin Essentials,有一些名为 Launcher 的功能。这是我的代码
await Launcher.TryOpenAsync("file:///" + localPath);
Run Code Online (Sandbox Code Playgroud)
我的本地路径是存储在Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);.
每当我尝试打开该文件时,都会出现错误:
file:////data/user/0/mypackagename/files/.local/share/Screenshot.jpg exposed beyond app through Intent.getData()
Run Code Online (Sandbox Code Playgroud)
我在 stackoverflow 上找到了几个解决方案,但我不想使用 Intent,因为我的应用程序被设计为跨平台的(如果可能的话,我想避免原生平台编码)。
Launcher 在 iOS 上也抛出错误:
canOpenURL: failed for URL: ** -- file:///" - error: "This app is not allowed to query for scheme file
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
Bra*_*ick 17
我们可以HttpClient用来下载图像。
HttpClient.GetByteArrayAsync 将检索图像数据并将其保存在内存中。
在DownloadImage下面,我们将检索图像作为byte[].
static class ImageService
{
static readonly HttpClient _client = new HttpClient();
public static Task<byte[]> DownloadImage(string imageUrl)
{
if (!imageUrl.Trim().StartsWith("https", StringComparison.OrdinalIgnoreCase))
throw new Exception("iOS and Android Require Https");
return _client.GetByteArrayAsync(imageUrl);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们已经下载了图像,我们将把它保存到磁盘。
Xamarin.Essentials.Preferences允许我们使用键值对将项目保存到磁盘。由于byte[]它只是一个指向内存的指针,我们必须先将它转换byte[]为 base64 字符串,然后才能将其保存到磁盘。
static class ImageService
{
static readonly HttpClient _client = new HttpClient();
public static Task<byte[]> DownloadImage(string imageUrl)
{
if (!imageUrl.Trim().StartsWith("https", StringComparison.OrdinalIgnoreCase))
throw new Exception("iOS and Android Require Https");
return _client.GetByteArrayAsync(imageUrl);
}
public static void SaveToDisk(string imageFileName, byte[] imageAsBase64String)
{
Xamarin.Essentials.Preferences.Set(imageFileName, Convert.ToBase64String(imageAsBase64String));
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们已经下载了图像并将其保存到磁盘,我们需要能够从磁盘检索图像以将其显示在屏幕上。
GetFromDisk下面从磁盘检索图像并将其转换为Xamarin.Forms.ImageSource.
static class ImageService
{
static readonly HttpClient _client = new HttpClient();
public static Task<byte[]> DownloadImage(string imageUrl)
{
if (!imageUrl.Trim().StartsWith("https", StringComparison.OrdinalIgnoreCase))
throw new Exception("iOS and Android Require Https");
return _client.GetByteArrayAsync(imageUrl);
}
public static void SaveToDisk(string imageFileName, byte[] imageAsBase64String)
{
Xamarin.Essentials.Preferences.Set(imageFileName, Convert.ToBase64String(imageAsBase64String));
}
public static Xamarin.Forms.ImageSource GetFromDisk(string imageFileName)
{
var imageAsBase64String = Xamarin.Essentials.Preferences.Get(imageFileName, string.Empty);
return ImageSource.FromStream(() => new MemoryStream(Convert.FromBase64String(imageAsBase64String)));
}
}
Run Code Online (Sandbox Code Playgroud)
ImageService在 a 中使用Xamarin.Forms.ContentPageclass App : Application
{
public App() => MainPage = new MyPage();
}
class MyPage : ContentPage
{
readonly Image _downloadedImage = new Image();
public MyPage()
{
Content = _downloadedImage;
}
protected override async void OnAppearing()
{
const string xamrainImageUrl = "https://cdn.dribbble.com/users/3701/screenshots/5557667/xamarin-studio-1_2x_4x.png"
const string xamarinImageName = "XamarinLogo.png";
var downloadedImage = await ImageService.DownloadImage(xamrainImageUrl);
ImageService.SaveToDisk(xamarinImageName, downloadedImage);
_downloadedImage.Source = ImageService.GetFromDisk(xamarinImageName);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7081 次 |
| 最近记录: |