2014-10-07 157 views
0

我有一個FileProvider工作得非常清楚,我能夠將文件分享給任何應用程序,這是我的代碼:FileProvider包括所有子文件夾

FilePaths.xml:

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <!-- choose between cache-path (cache storage), files-path (app-private storage) and external-path (external storage) --> 
    <cache-path path="/" name="strips" /> 
</paths> 

設置分享目的:

 File f = new File(_fileFullName); 

    var contentUri = FileProvider.GetUriForFile(this, 
     G.FileProviderAuthorityName, 
     f); 
    intent.PutExtra(Intent.ExtraStream, contentUri); 

    _shareProvider.SetShareIntent(intent); 

這很有效。我忘了在這裏提到我的文件通常位於應用程序緩存目錄的子文件夾內,無論它們在哪裏(文件夾都是動態創建的),它都可以工作。

但是當我改變了XML緩存路徑的文件路徑(AppPrivate存儲),我得到拋出:IllegalArgumentException:

無法找到包含 /存儲配置的根/模擬/ 0 /安卓/數據/ GetUriForFile調用中的app.namespace/files/subfolder/data.png。

我已經嘗試了FilePaths.xml中的所有變體,搜索了所有我能找到的答案。

回答

1

請忽略,發現我的問題。我使用getExternalFilesDir(null)而不是getFilesDir()來保存我的文件。

修正了通過閱讀android支持庫的源代碼。內部getUriForFile執行此代碼來決定使用哪個目錄:

File target = null; 
       if (TAG_ROOT_PATH.equals(tag)) { 
        target = buildPath(DEVICE_ROOT, path); 
       } else if (TAG_FILES_PATH.equals(tag)) { 
        target = buildPath(context.getFilesDir(), path); 
       } else if (TAG_CACHE_PATH.equals(tag)) { 
        target = buildPath(context.getCacheDir(), path); 
       } else if (TAG_EXTERNAL.equals(tag)) { 
        target = buildPath(Environment.getExternalStorageDirectory(), path); 
       } 

的自定義XML選擇最匹配的路徑,這意味着子文件夾不會有問題:

// Find the most-specific root path 
      Map.Entry<String, File> mostSpecific = null; 
      for (Map.Entry<String, File> root : mRoots.entrySet()) { 
       final String rootPath = root.getValue().getPath(); 
       if (path.startsWith(rootPath) && (mostSpecific == null 
         || rootPath.length() > mostSpecific.getValue().getPath().length())) { 
        mostSpecific = root; 
       } 
      } 
相關問題