10

Is it possible to have a FileProvider available to other applications ?

manifest.xml

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

From the doc:

false: The provider is not available to other applications. Set android:exported="false" to limit access to the provider to your applications. Only applications that have the same user ID (UID) as the provider will have access to it.

I've tried to set exported to true but I got this exception Unable to get provider android.support.v4.content.FileProvider: java.lang.SecurityException: Provider must not be exported Why I can't export a FileProvider ?

M'hamed
  • 2,508
  • 3
  • 24
  • 41

1 Answers1

21

Is it possible to have a FileProvider available to other applications ?

Yes. That is usually the point.

Why I can't export a FileProvider ?

Because that is not how you use a FileProvider.

The point behind FileProvider is to give select access to files to third-party apps. You can do this by FLAG_GRANT_READ_URI_PERMISSION and/or FLAG_GRANT_WRITE_URI_PERMISSION, in the Intent that you use to pass one of your provider's Uri values to the third-party app (e.g., via an ACTION_VIEW Intent used with startActivity()).

Also see the training guide on sharing files.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi. i used your advises and everything is fine but when i want to share soundfile using bluetooth an error occur and say bluetooth is stopped. i think it is because bluetooth has not permission to read file from my app folder. how can i solve this? Thanks. – karimkhan Dec 15 '15 at 17:44
  • 2
    this is my code:this is my code: sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(_context,"ir.khazan.ringtone.fileprovider",newSoundFile_in_app_folder)); sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); sendIntent.setType("audio/*"); Utils.activity.startActivity(sendIntent); – karimkhan Dec 15 '15 at 17:44
  • @karimkhan: "how can i solve this?" -- use LogCat to examine the Java stack trace associated with the other app. Ask a separate question on Stack Overflow, with your code and that stack trace, if you do not understand it. – CommonsWare Dec 15 '15 at 17:45