找不到路径“ /content:/com.android.externalstorage.documents/document/6759-130B%3ANew%20Text%20Document.txt”的一部分

问题描述

我正在使用FilePicker-Plugin-for-Xamarin-and-Windows,并且在访问外部存储设备时收到此错误

Could not find a part of the path "/content:/com.android.externalstorage.documents/document/6759-130B%3ANew%20Text%20Document.txt".

enter image description here

注意:我既拥有读写权限,又在运行时询问用户。
使用此路径问题时消失了:

storage/6759-130B%3ANew%20Text%20Document.txt

这是选择器中的非常知名的旧错误,为officially mentioned

对我来说,我似乎必须将 content:// 转换为文件路径

更多info

解决方法

我记得那件事,您需要从uri中获取绝对路径。通常,content://路径是从下载文件夹或任何驱动器路径返回的,您需要获取其实际路径。你可以试试看。

它是适用于Android的设备专用代码,请向其注入依赖项服务。

我在Native android中遇到了上述问题,并解决了Java中的问题,以下是c#中相同代码的转换后的代码

private string GetRealPathFromURI(Uri contentURI)
{
    ICursor cursor = ContentResolver.Query(contentURI,null,null);
    cursor.MoveToFirst();
    string documentId = cursor.GetString(0);
    documentId = documentId.Split(':')[1];
    cursor.Close();

    cursor = ContentResolver.Query(
    Android.Provider.MediaStore.Images.Media.ExternalContentUri,MediaStore.Images.Media.InterfaceConsts.Id + " = ? ",new [] { documentId },null);
    cursor.MoveToFirst();
    string path = cursor.GetString(cursor.GetColumnIndex(MediaStore.Images.Media.InterfaceConsts.Data));
    cursor.Close();

    return path;
}

编辑-

尝试一下

       FileData fileData = await CrossFilePicker.Current.PickFile();
       string filePath;
       if (fileData != null)
       {
            await Task.Run(() => {
            filePath = DependencyService.Get<IImageUtilities>().SaveFileFromStream(new MemoryStream(fileData.DataArray),fileData.FileName));
        });

Xamarin.Android的代码

public string SaveFileFromStream((System.IO.Stream imageStream,string filename)
{
            string name = filename;
            string filePath = null;
            try
            {
                byte[] imageData = ((MemoryStream)imageStream).ToArray();
                IFolder folder = FileSystem.Current.GetFolderFromPathAsync(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString()).Result;
                IFile file = folder.CreateFileAsync(name,CreationCollisionOption.GenerateUniqueName).Result;
                filePath = System.IO.Path.Combine(Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures).ToString(),file.Name);

                System.IO.Stream outputStream = file.OpenAsync(PCLStorage.FileAccess.ReadAndWrite).Result;
                outputStream.Write(imageData,imageData.Length);
                outputStream.Flush();
                outputStream.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return filePath;
}

稍后,文件的新路径将在相应需要的所有地方使用。

这是我在项目中实际执行的操作,无论用户选择了哪个文件,都将其复制到内部存储中的Pictures Dir中并返回路径。

我们处理ImageStream以制作原始文档的副本。 制作副本的想法是,我们将副本用于上传目的,因为用户可以删除所选的原始文档。因此,在将文档推送到服务器之后,我们也删除了复制的文件。因此,在处理流时,Content://不会遇到任何问题。

希望这可能有所帮助。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...