【问题标题】:I can not share video to whatsapp from my app我无法从我的应用程序将视频分享到 whatsapp
【发布时间】:2019-07-05 23:22:08
【问题描述】:

我有一个 Android Studio 项目,可以选择通过 whatsapp 分享视频。

这是我的代码:

        private void shareFile(File file){
        String titulo = file.getName();
        String path = file.getAbsolutePath();
        Uri uri = Uri.parse(path);
        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        shareIntent.setType("video/*");
        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, title);
        shareIntent.putExtra(android.content.Intent.EXTRA_TITLE, title);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(shareIntent);
        }

但是当我分享它时,我收到了来自 Whatsapp 的消息:此文件格式不兼容。 我不明白在它起作用之前会发生什么。

【问题讨论】:

  • Uri.fromFile() 已在 Android 7.0+ 上被禁止。使用FileProvider。此外,请使用真正的 MIME 类型,而不是通配符。

标签: java android share whatsapp


【解决方案1】:

ANDROIDMANIFEST.XML

<provider
    android:name="android.support.v4.content.FileProvider"
    android:grantUriPermissions="true"
    android:exported="false"
    android:authorities="${applicationId}">

    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_provider_paths"/>

</provider>

RES/xml 中的FILE_PROVIDER_PATHS.XML

<paths>
    <cache-path name="cache" path="/" />
    <files-path name=”files” path=”/” />
</paths>

使用您的文件提供者

// create new Intent
Intent intent = new Intent(Intent.ACTION_VIEW);

// set flag to give temporary permission to external app to use your FileProvider
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

// generate URI, I defined authority as the application ID in the Manifest, the last param is file I want to open
String uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, file);

// I am opening a PDF file so I give it a valid MIME type
intent.setDataAndType(uri, "video/*");

// validate that the device can open your File!
PackageManager pm = getActivity().getPackageManager();
if (intent.resolveActivity(pm) != null) {
    startActivity(intent);
}

【讨论】:

  • 它适用于手机内存中的文件,但适用于可移动卷中的文件我收到以下错误:IllegalArgumentException:找不到包含/storage/0000-0000/file的配置根.mp4
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-29
  • 2013-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-08
相关资源
最近更新 更多