gpt4 book ai didi

java - 我无法从我的应用程序将视频分享到 Whatsapp

转载 作者:行者123 更新时间:2023-12-02 03:25:18 24 4
gpt4 key购买 nike

我有一个 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 的消息:此文件格式不兼容。我不明白在它起作用之前会发生什么。

最佳答案

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);
}

关于java - 我无法从我的应用程序将视频分享到 Whatsapp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com