首页 > 解决方案 > Uno Platform Android 和 UWP:如何将文件复制到本地文件夹?

问题描述

我正在尝试选择图片文件并将它们复制到应用程序本地文件夹,并将路径保存为数据库中的字符串以显示图像,该代码在 UWP 上运行良好,但在 Android 上运行不正常(我使用的是 Uno 平台):

    string SImag = string.Empty;

    private async Task<string> MediaxPicAsync()
    {
        Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker();
        openPicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".bmp");
        openPicker.FileTypeFilter.Add(".gif");
        openPicker.FileTypeFilter.Add(".jpg");
        openPicker.FileTypeFilter.Add(".jpeg");
        openPicker.FileTypeFilter.Add(".png");
        Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync();

        if (file != null)
        {
            try
            {
                Windows.Storage.StorageFile temp = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("TempFile." + file.FileType);

                await file.CopyAndReplaceAsync(temp);
                return temp.Path;
            }
            catch (Exception ex)
            {
                await new Windows.UI.Popups.MessageDialog("Could not open image!", "Error image loading").ShowAsync();;
            }
        }
        return string.Empty;
    }

Xaml 代码:

<Image>
     <Image.Source>
         <BitmapImage UriSource="{x:Bind SImag, Mode=OneWay,Converter={StaticResource appClsConvStr2Imag}}" />
     </Image.Source>
</Image>

转换器:

public class ClsConvStr2Imag : Windows.UI.Xaml.Data.IValueConverter
    {
        object Windows.UI.Xaml.Data.IValueConverter.Convert(object value, Type targetType, object parameter, string language)
        {
            if (string.IsNullOrEmpty(value as string))
            {
                return null;
            }
            else
            {
                string sPath = (string)value;
                if (sPath.Contains("/") | sPath.Contains("\\"))
                {

                }
                else
                {
                    if (parameter == null)
                    {
                        sPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\" + sPath;
                    }
                    else
                    {
                        sPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path + "\\" + parameter + "\\" + sPath;
                    }
                }
                return new Uri(sPath);
            }
        }

        object Windows.UI.Xaml.Data.IValueConverter.ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }

在 Android 调试期间的 Visual Studio 输出中,它显示错误为:

[BitmapFactory] Unable to decode stream: java.io.FileNotFoundException: /data/user/0/Sale.Sale/files/TempFile.png (No such file or directory)

任何克服这个问题的帮助都是非常受欢迎的,我做了 3 天的研究,找不到这个问题的干净解决方案。非常感谢您的时间和专业知识。

标签: androidimageuno-platform

解决方案


推荐阅读