android.os.FileUriExposedException exposed beyond app through clipboard Intent.getData()

 If you encounter this error then you're in right place to solve the issue. Please follow the below instruction to solve the problem.

First of all why this error happen, it's because trying to share a file:// Uri in intent broadcast to share data with other apps on Android 24 or later version. 


Step 1: create a .xml file at xml folder within res folder. File content would like below:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

Step 2: Paste following code inside application tag at Android manifest file.


<application>

    <application>
.....
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>

Step 3: New way to share your broadcast intent:


final Intent intent = new Intent(     android.content.Intent.ACTION_SEND);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.putExtra(android.content.Intent.EXTRA_TEXT,  getString(R.string.sharetext)+" Download App Link :  https://play.google.com/store/apps/details?id="+con.getPackageName());


// old way to create uri


intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));


// end of old way to create uri

            

// New way to solve problem

            Uri myPhotoFileUri = FileProvider.getUriForFile(con, getApplicationContext().getPackageName() + ".provider", file);

            intent.putExtra(Intent.EXTRA_STREAM, myPhotoFileUri);

            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            // end new way



            intent.setType("image/jpg");

            startActivity(intent);




That should solve the problem now.

Happy coding...



Post a Comment

0 Comments