android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()
If your targetSdkVersion >= 24
, then we have to use FileProvider
class to give access to the particular file or folder to make them accessible for other apps. We create our own class inheriting FileProvider
in order to make sure our FileProvider doesn't conflict with FileProviders declared in imported dependencies as described here.
Steps to replace file://
URI with content://
URI:
- Add a FileProvider
<provider>
tag inAndroidManifest.xml
under<application>
tag. Specify a unique authority for theandroid:authorities
attribute to avoid conflicts, imported dependencies might specify${applicationId}.provider
and other commonly used authorities.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<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>
</manifest>
- Then create a
provider_paths.xml
file inres/xml
folder. A folder may be needed to be created if it doesn't exist yet. The content of the file is shown below. It describes that we would like to share access to the External Storage at root folder(path=".")
with the name external_files.
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
The final step is to change the line of code below in
Uri photoURI = Uri.fromFile(createImageFile());
to
Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", createImageFile());
Edit: If you're using an intent to make the system open your file, you may need to add the following line of code:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Please refer to the full code and solution that have been explained here.
FileUriExposedException in Android
I found solution here
can we open download folder via. intent?
This code works perfect in my Samsung J7 to open Pictures folder (and others) from internal memory using Samsung default application My files.
File path = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);
Uri uri = Uri.fromFile(path);
Intent intent = getPackageManager().getLaunchIntentForPackage("com.sec.android.app.myfiles");
intent.setAction("samsung.myfiles.intent.action.LAUNCH_MY_FILES");
intent.putExtra("samsung.myfiles.intent.extra.START_PATH", path.getAbsolutePath());
startActivity(intent);
It seems like we have to decompile File Manager of each manufacturer to see how to call it properly. :( And it is too much work. Well... I assumed there is some generic solution to do it.
android.os.FileUriExposedException: exposed beyond app through Intent.getData() Exception , While sharing VCFile
Try below code it works for sharing VCFile:
private void shareContact(ContactsModal modal) {
String lookupKey = null;
Cursor cur = ((Activity)context).getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, new String[]
{ ContactsContract.Contacts.LOOKUP_KEY },
ContactsContract.Contacts._ID + " = " + modal.getContactID(), null, null);
if (cur.moveToFirst()) {
lookupKey = cur.getString(0);
}
if(lookupKey!=null){
Uri vcardUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(ContactsContract.Contacts.CONTENT_VCARD_TYPE);
intent.putExtra(Intent.EXTRA_STREAM, vcardUri);
intent.putExtra(Intent.EXTRA_SUBJECT, "Contact Name");
((Activity)context).startActivity(intent);
}
}
android.os.FileUriExposedException being caused in Oreo (only!)
Try adding this inside onCreate.
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
Update app using Intent fails with FileUriExposedException
I think you are missing Intent.FLAG_GRANT_WRITE_URI_PERMISSION, it will grant rights to the component that eventually handles the request and its Intent.
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
promptInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
this.context.startActivity(promptInstall);
And change
Uri uri = Uri.fromFile(outputFile);
to
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",outputFile);
Hope this will solve your issue...
Read more about this in commonsware blog. It has been has explained clearly on this : https://commonsware.com/blog/2016/08/31/granting-permissions-uri-intent-extra.html
Android App is crashing because of android.os.FileUriExposedException
don't use Uri.fromFile()
you are using fileprovider so use FileProvider.getUriForFile(Context context, String authority, File file)
otherwise what is the use of file provider.
FileProvider doc -> https://developer.android.com/reference/androidx/core/content/FileProvider
Android android.os.FileUriExposedException Error in Android N
You must use FileProvider when you want to share files to other apps.
check https://developer.android.com/training/secure-file-sharing/setup-sharing.
use FileProvider.getUriForFile to get Uri instead of Uri.fromFile(file)
in you code.
FileProvider.getUriForFile(this,"your_package.fileprovider",photoFile);
FileUriExposedException using Android 7
If your targetSdkVersion is 24 or higher, we have to use FileProvider
class to give access to the particular file or folder to make them
accessible for other apps. We create our own class inheriting
FileProvider in order to make sure our FileProvider doesn't conflict
with FileProviders declared in imported dependencies as described
here.
Find relevant discussion here
Since Android 7, we don't use file: scheme as uri for intent, you have to use FileProvider.
Related Topics
Httpurlconnection.Getresponsecode() Returns -1 on Second Invocation
Android Gives Error "Cannot Fit Requested Classes in a Single Dex File"
Problems Importing Project into Android Studio Regarding Actionbarsherlock
Android Intent for Sending Email with Attachment
How to Resolve "Duplicate Files Copied in APK Meta-Inf/*"
Divide Elements on Groups in Recyclerview or Grouping Recyclerview Items ,Say by Date
Return Data from Asynctask Class
Online Radio Streaming App for Android
Android: Specify Two Different Images for Togglebutton Using Xml
What Does Transitive = True in Gradle Exactly Do (W.R.T. Crashlytics)
Android App Bundle Introduces Resource Not Found Crash in Android App
How to Show Ellipses on My Textview If It Is Greater Than the 1 Line
How to Simulate Low Memory in the Android Emulator