2016-11-12 38 views
3

我在应用程序中使用FileProvider时,我从应用程序拍摄照片时存储在那里。还有我的应用程序有不同的应用程序ID用于调试和发布版本如何为具有动态应用程序的应用程序指定路径外部路径ID

  • com.rkmax.myapp
  • com.rkmax.myapp.debug

我定义我的文件提供这样

<provider 
    android:name="android.support.v4.content.FileProvider" 
    android:authorities="@string/authority_file_provider" 
    android:exported="false" 
    android:grantUriPermissions="true"> 
    <meta-data 
     android:name="android.support.FILE_PROVIDER_PATHS" 
     android:resource="@xml/file_paths" /> 
</provider> 

@string/authority_file_provider值将变成:

  • com.rkmax.myapp.fileprovider
  • com.rkmax.myapp.debug.fileprovider

和我@xml/file_paths的定义如下

<?xml version="1.0" encoding="utf-8"?> 
<paths> 
    <external-path name="my_images" path="Android/data/com.rkmax.myapp/files/Pictures" /> 
</paths> 

,如果我尝试改变像Picturesfiles/Pictures我的应用程序失败

Failed to find configured root that contains/storage/emulated/0/Android/data/com.rkmax.myapp.debug/files/Pictures/20161112_073128-251269360.jpg 

我如何定义文件提供程序路径中的相对路径?

回答

6

为您的特定情况下,更换:

<external-path name="my_images" path="Android/data/com.rkmax.myapp/files/Pictures" /> 

有:

<external-files-path name="my_images" path="Pictures" /> 

这将需要24.0.0或更高版本的支持库,IIRC。

+0

同样的错误。在试用错误后,我得到这个工作,用'替换路径'。' – rkmax

+0

@rkmax:对于你的问题中的特定文件,这不应该是必要的AFAIK。可能还有其他文件正在服务,而不是在'Pictures /'中,对于这些文件您需要'.'作为路径。 – CommonsWare

+0

我也试过用'./ Pictures'没有成功 – rkmax