androidandroid-intentandroid-fileproviderandroid-internal-storage

Setup FileProvider from getFilesDir() which can access by others app?


Suppose I have a app A (com.xxx.aaa) has file provider come from getFilesDir() Having xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="files" path="."/>
</paths>

AndroidManifest:

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

On other app B (com.xxx.bbb) would like to ask app A to make some process on its file from getFilesDir(), assume app A already know app B filename (target.txt)

try{
    Intent intent = new Intent("com.xxx.aaa.DO_SOMETHING_ON_TARGET");
    intent.setClassName("com.xxx.aaa","com.xxx.aaa.TargetActivity");
    File file = new File("/data/data/com.xxx.aaa/files/target.txt");
    Uri contentUri = FileProvider.getUriForFile(context, "com.xxx.aaa.fileprovider", file);
    intent.setData(contentUri);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    Log.d(TAG, "setted fileprovider uri: "+contentUri);
    context.startActivity(intent);
}catch(Exception e){
    Log.e(TAG, "getUriForFile failed", e);
}

It would output exception:

IllegalArgumentException: Failed to find configured root /data/data/com.xxx.aaa/files/target.txt

Is this approach only work within one app? And I have no choice to define custom key which both app understands and use it on intent.putExtra(key, ...)?


Solution

  • Is this approach only work within one app?

    FileProvider.getUriForFile() is for the app's own FileProvider. App B does not have a FileProvider, let alone one configured for App A. Moreover, App B does not have access to the file, and therefore it cannot grant permission to any other app to access that file, as you are trying to do with FLAG_GRANT_READ_URI_PERMISSION.

    And I have no choice to define custom key which both app understands and use it on intent.putExtra(key, ...)?

    That would seem to be a straightforward approach.