1

I want to share a file /data/data/com.myapp/app_profiles/profile_1/games/game_1.xml via ACTION_SEND.

I've added this to the Manifest:

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.myapp.fileprovider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/filepaths" />
</provider>

and my filepaths.xml is

<paths>
    <files-path path="app_profiles/profile_1/games/" name="myGame" />
</paths>

the Code (in class XYActivity) is:

AlertDialog.Builder builder = new AlertDialog.Builder(this);    
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        ....
        File file = FileManager.getGameFile(gameID);
        Uri fileUri = FileProvider.getUriForFile(XYActivity.this,
                                "com.myapp.fileprovider", file);

I get the following error:

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.myapp/app_profiles/profile_1/games/game_1.xml
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
    ...

I have checked with the file explorer, this file does exist!

About the file object I know

file.getPath()         /data/user/0/com.myapp/app_profiles/profile_1/games/game_1.xml
file.getAbsolutePath() /data/user/0/com.myapp/app_profiles/profile_1/games/game_1.xml
file.getName()         game_1.xml
file.getParent()       /data/user/0/com.myapp/app_profiles/profile_1/games

For the path in filepaths.xml I've tried different, even wrong, values:

path="app_profiles/profile_1/gamXes/"  
path="app_profiles/profile_1/games/"  
path="app_profiles/profile_1/games"  
path="profile_1/games"  
path="games/"  
path="games"
path="."
path="../games/"

It doesn't change a thing. This would suggest that they're all wrong and I just have to use the right value, but I don't what that would be/why mine doesn't work.

There are several similar answers and they don't work for me

no subdirectories: FileProvider error "Failed to find configured root that contains /data/data/sawbodeployer.entm.illinois.edu ..."
external storage: File Provider: Failed to find configured root that contains, java.lang.IllegalArgumentException: Failed to find configured root that contains, many more

RKRK
  • 1,284
  • 5
  • 14
  • 18
peer
  • 4,171
  • 8
  • 42
  • 73

1 Answers1

1

/data/data/com.myapp/app_profiles/profile_1/games/game_1.xml is not a standard location, and FileProvider does not support it. <files-path> is for getFilesDir(), which would map to /data/data/com.myapp/files/, and your file is not inside of there.

Either:

  • Store the files in a better spot (e.g., a directory off of getFilesDir()), or

  • Write your own ContentProvider that can serve files from your desired location

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491