Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

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

I am using FilePicker-Plugin-for-Xamarin-and-Windows and got this error while accessing external storage:

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

enter image description here

Note:I have both read and write permissions And also I ask user at runtime.
When using this path problem gone:

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

This is very known old bug in picker as officially mentioned.

For me it looks like I have to convert content:// into file path.

More info

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I remembered the thing, You need to get the absolute path from your uri. Usually, content:// path is returned from download folder or any drive path, you need to get actual path for it. You can try this.

Its an device specific code for Android, Inject it with dependency service.

I faced above issue in Native android & solved that as follows in Java, below is the converted code for the same in c#

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

    cursor = ContentResolver.Query(
    Android.Provider.MediaStore.Images.Media.ExternalContentUri,
    null, 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;
}

EDIT -

Try this

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

Code for 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, 0, imageData.Length);
                outputStream.Flush();
                outputStream.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            return filePath;
}

Later on, the new path of the file is used everywhere needed accordingly.

This is what I actually do in my project, whichever file is selected by the user, it is copied to Pictures Dir in internal storage & returns the path.

We deal with the ImageStream to make a copy of the original document. The idea to make a duplicate copy is that we uses the copy for uploading purpose as the user may delete the original document selected. So after pushing the document to the server, we delete the copied file as well. So as we deal with the stream we don't face any issue with Content://.

Hope this maybe helpful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...