FileUriExposedException in Android [duplicate]

Is it possibility to use My Files app by default to open certain folder?

Yes & No. Its not 100% guaranteed that it will work on all devices.

Edit 1:

Following is one of the way with which it can be done. I have tested on few emulators (running Android N & Android O) and loads default file explorer:

MainActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri dirUri = FileProvider.getUriForFile(this,getApplicationContext().getPackageName() + ".com.example.myapplication",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        //intent.setDataAndtType(); I will change this value in the alternatives below
    }

AndroidManifest.xml

<provider
        android:name=".GenericFileProvider"
        android:authorities="${applicationId}.com.example.myapplication"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

GenericFileProvider.java

public class GenericFileProvider extends FileProvider {
}

file_paths.xml

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

The above approach doesn’t work on big players like samsung

Alternatives

1. Using type DocumentsContract.Document.MIME_TYPE_DIR

intent.setDataAndType(dirUri,DocumentsContract.Document.MIME_TYPE_DIR);

This approach works on several emulators and limited set of devices. It doesn’t work with big players like Samsung or Huawei.

2. Using type resource/folder

intent.setDataAndType(dirUri,"resource/folder");

This approach works only if user has installed ES file explorer app.

If you choose to use , then you have to check if any intent is available to handle it by using:

PackageManager packageManager = getActivity().getPackageManager();

if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
} else {
   // either display error or take necessary action
}

3. Using type */*

intent.setDataAndType(dirUri,"*/*");

This approach works, if user chooses File Manager app from the Intent Chooser and mark it as default app to handle */*. However it has some drawbacks (Thanks to @CommonsWare for bringing some of it out):

  • This type will load all the apps on the device and allow user to choose one of them to complete the action.
  • If there is no file explorer and user chooses other apps to load your intent, then the other app will crash or simply show black screen. E.g. You use Gallery or some other app to launch it rather than file explorer, then Gallery app will either crash or show black screen.
  • Even if there is file explorer but user decides to use other apps, the other apps could crash

4. Using type text/csv

intent.setDataAndType(uri, "text/csv")

This will limit the number of apps which will displayed to the user but same limitations when */* used is applied. Apps which can handle csv will be displayed and if user chooses it then the apps would crash.

There are some device specific implementations available here as mentioned by @Academy of Programmer which requires to identify the default file manager’s intent and extra’s need by it.

Conclusion:

There is no standard type available to achieve it since there is no standard followed by the File Managers to support specific type at the moment. In future may be Google will come up with some approach. Best alternative would be to implement your own file manager just like Dropbox or Google Drive does. There are several libraries available which provide this feature.

Leave a Comment