如何解决找不到路径“ /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".
注意:我既拥有读写权限,又在运行时询问用户。
使用此路径问题时消失了:
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://
不会遇到任何问题。
希望这可能有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。