2017-07-28 51 views
0

我想打開特殊文件夾,用戶只能選擇.xml文件。所以,我爲用戶使用了SetDataAndType。一切工作正常在新版本的android(測試api 21 .. 25)。但它不爲API 16.在這裏工作是代碼我使用:在舊版本的android中打開特殊文件夾視圖意圖

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); 
      } 

同一窗口中顯示每

時間

enter image description here

+0

任何堆棧跟蹤? –

+0

nope,nothing:/ – Nininea

+0

特殊文件夾在哪裏? –

回答

0

試試這個

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; 
} 
相關問題