Skip to content
Advertisement

Downloaded file share to other app like Whasapp, Facebook from my app

THIS error show my code i try set share option in my app for downloaded video

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/VideoDownloadFAST/20200816190612.mp4 at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:744) at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418) at com.example.appname.HomeActivity$1$1.onMenuItemClick(HomeActivity.java:101)

path file code

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

AndroidManifest file code

<provider android:name="androidx.core.content.FileProvider" android:authorities="com.example.appname.provider"
  android:exported="false" android:grantUriPermissions="true">
  <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>


Intent share = new Intent(Intent.ACTION_SEND);
share.setType("video/mp4");
share.putExtra(Intent.EXTRA_SUBJECT, "abc");
share.putExtra(Intent.EXTRA_TITLE, "abcd");
File imageFileToShare = new File(Environment.getExternalStorageDirectory() + "/VideoDownloadFAST/" + name);
Uri uri = FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", imageFileToShare);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setPackage("com.example.appname");
startActivity(Intent.createChooser(share, "Message"));

Advertisement

Answer

Change your provider.xml to a broader extend which is more convenient:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="external_files"
        path="." />
</paths>

And then in java:

    public static void shareVideo(Context context, String fullPath) {
    try {
        File file = new File(fullPath);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Uri videoUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM, videoUri);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setData(videoUri);
            intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "share title"));
        } else {
            Uri videoUri = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.putExtra(Intent.EXTRA_STREAM, videoUri);
            intent.setDataAndType(videoUri, "video/*");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "share title"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Don’t forget to add this to your AndroidManifest.xml file:

<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>
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement