从我的应用程序下载文件共享到其他应用程序,如 Whatsapp、Facebook

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

此错误显示我的代码我尝试在我的应用程序中为下载的视频设置共享选项

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.onMenuItemClick(HomeActivity.java:101)

路径文件代码

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

将您的 provider.xml 更改为更方便的更广泛的扩展:

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

然后在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();
    }
}

别忘了将此添加到您的 AndroidManifest.xml 文件中:

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