繁体   English   中英

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

[英]Get path from URI on external sd-card in Xamarin

我刚刚开始使用 Xamarin,我对 Android 的经验有限。

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

从 DocumentTree 收到的 Uri 是这样形成的: content://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”,所以它不会被处理。

当我选择内部存储时,我得到了 Uri content://com.android.externalstorage.documents/tree/primary%3ATest然后给出了 /storage/emulated/0/Test 的路径,因为类型被设置为“主要”。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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