Android.Os.Fileuriexposedexception: File:///Storage/Emulated/0/Test.Txt Exposed Beyond App Through Intent.Getdata()

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 in AndroidManifest.xml under <application> tag. Specify a unique authority for the android: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 in res/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.

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);
}
}

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.

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 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);

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();


Related Topics



Leave a reply



Submit