从Xamarin的外部SD卡上的URI获取路径

时间:2018-03-01 22:08:44

标签: android xamarin filepath sd-card

我刚刚开始与Xamarin合作,我对Android的经验有限。

我已经触发并与ActionOpenDocumentTree意图获取我的应用程序文件的“保存路径”。典型的选择器是 like this

从DocumentTree收到的Uri形成如下:内容://com.android.externalstorage.documents/tree/12FB-3215%3ATest

这是我使用的函数,它来自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?

在这种情况下,变量“type”是“12FB-3215”而不是“primary”,因此它不会被处理。

When I choose我得到的内部存储空间内容://com.android.externalstorage.documents/tree/primary%3ATest 然后给出/ storage / emulated / 0 / Test的路径,因为type被设置为“primary”。

那么ID为12FB-3215的SD卡的正确路径是什么? 是/ storage / 12FB-3215 /测试还是/ storage / emulated / 12FB-3215 /测试?

对于没有经验的Android编码器有什么好的建议吗?

1 个答案:

答案 0 :(得分:0)

在Xamarin中,您可以使用我的库访问android上的任何存储。但适用于支持存储访问框架字(SAF)的Android 5+ https://github.com/madnik7/PortableStorage