微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从我的应用程序将文件共享下载到其他应用程序,如Whasapp,Facebook

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

java.lang.IllegalArgumentException:无法找到已配置的根 包含/storage/emulated/0/VideodownloadFAST/20200816190612.mp4 在androidx.core.content.FileProvider $ SimplePathStrategy.getUriForFile(FileProvider.java:744) 在androidx.core.content.FileProvider.getUriForFile(FileProvider.java:418) 在com.example.appname.HomeActivity $ 1 $ 1.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文件代码

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。