Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Fixed FileUriExposed error on Nougat properly #118

Merged
merged 10 commits into from
Nov 9, 2016
Merged

Fixed FileUriExposed error on Nougat properly #118

merged 10 commits into from
Nov 9, 2016

Conversation

spacecowboy
Copy link
Owner

@spacecowboy spacecowboy commented Oct 22, 2016

fixes #107
fixes #115

changelog: You are now required to define a FileProvider in your manifest for the SD-card picker

Due to recent changes in Android 7.0 Nougat, bare File URIs can no longer be returned in a safe way. This change requires you to add an entry to your manifest to use the included FilePickerFragment and change how you handle the results.

  • You need to add the following to your app's AndroidManifest.xml:
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/nnf_provider_paths" />
        </provider>
  • Then you must change your result handling. Here is a code snippet illustrating the change for a single result (the same applies to multiple results):
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    // The URI will now be something like content://PACKAGE-NAME/root/path/to/file
    Uri uri = intent.getData();
    // A new utility method is provided to transform the URI to a File object
    File file = com.nononsenseapps.filepicker.Utils.getFileForUri(uri);
    // If you want a URI which matches the old return value, you can do
    Uri fileUri = Uri.fromFile(file);
    // Do something with the result...
}

This change was required in order to fix FileUriExposedException being thrown on Android 7.0 Nougat, as reported in #115 and #107.

Please see the updated activity in the sample app for more examples.

@hrsalehi
Copy link

this approach is not working. before returning to the onActivityResult I get FileUriExposedException

@spacecowboy
Copy link
Owner Author

@hrsalehi do you have any code to demonstrate a case where it doesn't work? Are you sure you are using the 4.0.0-beta1 version?

@hrsalehi
Copy link

hrsalehi commented Oct 27, 2016

here is the stack trace :
the error raised before returning to the onActivityResult
I am using version 3.1.0

android.os.FileUriExposedException: file:///storage/emulated/0/1470943459135.jpg exposed beyond app through ClipData.Item.getUri() at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799) at android.net.Uri.checkFileUriExposed(Uri.java:2346) at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832) at android.content.Intent.prepareToLeaveProcess(Intent.java:8909) at android.content.Intent.prepareToLeaveProcess(Intent.java:8894) at android.app.Activity.finish(Activity.java:5188) at android.app.Activity.finish(Activity.java:5208) at com.nononsenseapps.filepicker.AbstractFilePickerActivity.onFilesPicked(AbstractFilePickerActivity.java:165) at com.nononsenseapps.filepicker.AbstractFilePickerFragment.onClickOk(AbstractFilePickerFragment.java:309) at com.nononsenseapps.filepicker.AbstractFilePickerFragment$2.onClick(AbstractFilePickerFragment.java:196) at android.view.View.performClick(View.java:5609) at android.view.View$PerformClick.run(View.java:22262) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

I wrote a file provider section in the manifest, but in android 7.0 nothing works.

@hrsalehi
Copy link

I just tested the 4.0.0-beta1 version and the problem is solved. Thanks
But is there any consideration in this version? do I need to write a FileProvider in the manifest?

@spacecowboy
Copy link
Owner Author

@hrsalehi yes, you need to add a provider to your manifest, as is described in the PR description above. And then the result handling code will have to change little, which is also described above.

If anything is unclear, please let me know and I'll make sure to update the documentation better before the real release

@spacecowboy spacecowboy merged commit 3c61a39 into master Nov 9, 2016
@spacecowboy spacecowboy deleted the 4-nougat branch November 9, 2016 22:07
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FileUriExposedException on Android 7 SDK 24 - android.os.FileUriExposedException
2 participants