【问题标题】:Open special folder view intent in old version of android在旧版本的android中打开特殊的文件夹视图意图
【发布时间】:2017-07-28 08:58:56
【问题描述】:

我正在尝试打开用户只能选择 .xml 文件的特殊文件夹。所以,我使用 SetDataAndType 作为意图。在新版本的 android 中一切正常(经过测试的 api 21.. 25)。但它不适用于 api 16。这是我使用的代码:

 var intent = new Intent();

            Android.Net.Uri contentURI = null;

            if (_existingConfig != null)
            {
                if (requestCode == ASSETS_SELECT_CODE && !string.IsNullOrEmpty(_existingConfig.AssetsPath))
                {
                    contentURI = Android.Net.Uri.FromFile(new Java.IO.File(_existingConfig.AssetsPath));
                }
                else if (!string.IsNullOrEmpty(_existingConfig.GoodsPath))
                {
                    contentURI = Android.Net.Uri.FromFile(new Java.IO.File(_existingConfig.GoodsPath));
                }
            }

            if (contentURI != null)
            {
                intent.SetDataAndType(contentURI, "text/xml");
            }
            else
            {
                intent.SetType("text/xml");
            }

            if (Build.VERSION.SdkInt < BuildVersionCodes.Kitkat)
            {
                intent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(Intent.CreateChooser(intent, Resources.GetString(Resource.String.select_xml_file)),
                                        requestCode);
            }
            else
            {
                intent.SetAction(Intent.ActionOpenDocument);
                StartActivityForResult(intent, requestCode);
            }

每次都显示相同的窗口

【问题讨论】:

  • 任何堆栈跟踪?
  • 不,没什么:/
  • 特殊文件夹在哪里?

标签: android android-intent xamarin xamarin.android


【解决方案1】:

试试这个

    String path = "Your Path";
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(getFileUri(path.trim()), getMimeType(path.trim()));
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);


    /**
      * Determine the MIME type for the document file
      *
      * @param url Complete path of the file
      * @return string type of MIME
      */
     private static String getMimeType(String url) {
         String type = null;
         String extension = MimeTypeMap.getFileExtensionFromUrl(url);
         if (extension != null) {
             type =           
  MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}

/**
 * Generate URI for the required Intent
 *
 * @param filePath Path of the required file
 * @return uri
 */
 private static Uri getFileUri(String filePath) {
    Uri fileUri = null;
    File file = new File(filePath);
    try {
        if (Build.VERSION.SDK_INT >= BUILD_VERSION) {
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }
        fileUri = Uri.fromFile(file);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return fileUri;
}

【讨论】:

    猜你喜欢
    • 2017-01-21
    • 2014-02-27
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 2012-02-02
    相关资源
    最近更新 更多