简体   繁体   中英

Get path from URI on external sd-card in Xamarin

I've just started working with Xamarin and I have limited experience with Android.

I have triggered and Intent with ActionOpenDocumentTree to get a "save path" for files for my app. Typical selector is like this

The Uri received from DocumentTree is formed like this: content://com.android.externalstorage.documents/tree/12FB-3215%3ATest

Here is the function I use, it is from Filepicker-code:

    public static string GetActualPathFromFile(Context thisContext, Android.Net.Uri uri)
    {
        bool isKitKat = Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Kitkat;

        if (isKitKat && DocumentsContract.IsDocumentUri(thisContext, uri))
        {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri))
            {
                string docId = DocumentsContract.GetDocumentId(uri);

                char[] chars = { ':' };
                string[] split = docId.Split(chars);
                string type = split[0];

                if ("primary".Equals(type, StringComparison.OrdinalIgnoreCase))
                {
                    return Android.OS.Environment.ExternalStorageDirectory + "/" + split[1];
                }

                //How to handle other than primary?

Variable "type" in this case is "12FB-3215" and not "primary", so it does not get handeled.

When I choose the internal storage I get Uri content://com.android.externalstorage.documents/tree/primary%3ATest Which then are given a path of /storage/emulated/0/Test since type is set as "primary".

So what is the correct path for the SD-card with ID 12FB-3215? Is it /storage/12FB-3215/Test or /storage/emulated/12FB-3215/Test ?

Any good tips for an inexperienced Android-coder?

In Xamarin you can use my library to access any storage on android. But works on Android 5+ which support Storage-Access-Frameword (SAF) https://github.com/madnik7/PortableStorage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM