You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Uno Platform安卓与UWP端如何将选中图片复制到应用本地文件夹?

解决方案

核心问题原因

  1. 文件名拼接错误:StorageFile.FileType本身包含前缀.,原有代码拼接后会生成TempFile..png这类错误文件名,导致文件实际未正常创建
  2. 路径分隔符不兼容:转换器中硬编码Windows平台的\作为路径分隔符,Android平台使用/,拼接路径无效
  3. Uri格式不符合Android要求:本地文件路径需要添加file://协议头才能被Android的图像解析器识别
  4. 文件创建策略问题:未指定重名文件覆盖规则,重复选择同后缀文件时会触发创建失败异常

修改步骤

1. 修改图片选择逻辑

调整文件创建参数,修复文件名拼接错误,指定重名覆盖策略:

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
        {
            // 去掉FileType的前缀点,指定重名文件直接覆盖
            Windows.Storage.StorageFile temp = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync(
                "TempFile." + file.FileType.TrimStart('.'), 
                Windows.Storage.CreationCollisionOption.ReplaceExisting);

            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;
}

2. 修改路径转换器

使用系统API自动适配路径分隔符,生成符合Android要求的本地文件Uri:

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;
        }
        string sPath = (string)value;
        // 替换按位或为逻辑或,使用Path.Combine自动适配系统分隔符
        if (!(sPath.Contains("/") || sPath.Contains("\\")))
        {
            string rootPath = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
            if (parameter == null)
            {
                sPath = System.IO.Path.Combine(rootPath, sPath);
            }
            else
            {
                sPath = System.IO.Path.Combine(rootPath, parameter.ToString(), sPath);
            }
        }
        // 添加file://协议头,适配Android本地文件读取规则
        return new Uri($"file://{sPath}", UriKind.Absolute);
    }

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

3. 配置Android权限

在Android项目的AndroidManifest.xml中添加存储权限:

<!-- 适配安卓12及以下版本 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="32" />
<!-- 适配安卓13及以上版本的图片读取权限 -->
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

内容的提问来源于stack exchange,提问作者none

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 23:45:03