我刚开始使用Xamarin,但对Android的使用经验有限。
我已经触发了ActionOpenDocumentTree并使用Intent来获取我应用程序文件的“保存路径”。典型的选择器是like this从DocumentTree收到的Uri的格式如下:content://com.android.externalstorage.documents/tree/12FB-3215%3ATest
这是我使用的功能,来自Filepicker代码:
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?
在这种情况下,变量“类型”是“ 12FB-3215”,而不是“主要”,因此不会被处理。
When I choose我得到Uri的内部存储content://com.android.externalstorage.documents/tree/primary%3ATest由于类型设置为“ primary”,因此该路径的路径为/ storage / emulated / 0 / Test。
那么ID为12FB-3215的SD卡的正确路径是什么?是/ storage / 12FB-3215 / Test还是/ storage / emulated / 12FB-3215 / Test?
没有经验的Android编码者有什么好的秘诀?
在Xamarin中,您可以使用我的库来访问android上的任何存储。但适用于支持存储访问框架字(SAF)的Android 5+https://github.com/madnik7/PortableStorage