将下载的文件从我的应用程序分享到其他应用程序,比如WhatsApp、Facebook。

huangapple go评论86阅读模式
英文:

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

问题

java.lang.IllegalArgumentException: 无法找到包含 /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)

路径文件代码:

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

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;paths xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;&gt;
  &lt;external_files name=&quot;external_files&quot; path=&quot;.&quot; /&gt;
&lt;/paths&gt;

> AndroidManifest file code

&lt;provider android:name=&quot;androidx.core.content.FileProvider&quot; android:authorities=&quot;com.example.appname.provider&quot;
  android:exported=&quot;false&quot; android:grantUriPermissions=&quot;true&quot;&gt;
  &lt;meta-data android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot; android:resource=&quot;@xml/provider_paths&quot; /&gt;
&lt;/provider&gt;


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

答案1

得分: 0

改变你的 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(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(Intent.createChooser(intent, "分享标题"));
        } 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, "分享标题"));
        }
    } 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>
英文:

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;paths&gt;
    &lt;external-path
        name=&quot;external_files&quot;
        path=&quot;.&quot; /&gt;
&lt;/paths&gt;

And then in java:

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

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

&lt;provider
        android:name=&quot;androidx.core.content.FileProvider&quot;
        android:authorities=&quot;${applicationId}.provider&quot;
        android:exported=&quot;false&quot;
        android:grantUriPermissions=&quot;true&quot;&gt;
        &lt;meta-data
            android:name=&quot;android.support.FILE_PROVIDER_PATHS&quot;
            android:resource=&quot;@xml/provider_paths&quot; /&gt;
 &lt;/provider&gt;

huangapple
  • 本文由 发表于 2020年8月17日 13:02:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63444813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定