0

我想使用 Android Intent 系统实现分享视频到 WhatsApp 和其他应用程序功能。过去 2 天我一直在寻找它,但我没有得到正确的解决方案,而且我在 StackOverFlow 上找到了一些解决方案,我猜它们不再有效。

每当共享意图打开时,我单击 WhatsApp 并选择要共享的联系人,然后它说不支持文件格式

下面是我的代码:

ContentValues content = new ContentValues(4);
                        content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
                                System.currentTimeMillis() / 1000);
                        content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
                        content.put(MediaStore.Video.Media.DATA, "/storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20210822-WA0002.mp4");
                        ContentResolver resolver = getBaseContext().getContentResolver();
                        Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);

                        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                        sharingIntent.setType("video/*");
                        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
                        sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
                        startActivity(Intent.createChooser(sharingIntent,"share:"));
4

1 回答 1

0

这对我有用,请尝试。

videoPath = "file:///storage/emulated/0/WhatsApp/Media/WhatsApp Video/VID-20210822-WA0002.mp4"

调用这个函数:

shareImageBitmap(this, videoPath, "com.whatsapp");

分享视频功能:

public static void shareVideoBitmap(Activity activity, String sharedVideoPath, String pkgName) {
 
                    ArrayList<Uri> list = new ArrayList<>();
                    list.add(FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", new File(sharedVideoPath.replace("file://", "").trim())));

                    Intent intentImage = new Intent(Intent.ACTION_SEND);
                    intentImage.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intentImage.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", new File(sharedVideoPath.replace("file://", "").trim())));
                    intentImage.setType("video/*");

                    if (pkgName.length() > 0) {
                        intentImage.setPackage(pkgName);
                    }
                    intentImage.putExtra(Intent.EXTRA_TEXT,
                            String.format(activity.getString(R.string.share_email_body), activity.getString(R.string.app_name))
                                    + " " + APP_PLAYSTORE_LINK + activity.getPackageName());
                    ActivityInfo activityInfo = intentImage.resolveActivityInfo(activity.getPackageManager(), intentImage.getFlags());
                    if (activityInfo != null && activityInfo.exported) {
                        activity.startActivity(Intent.createChooser(intentImage, "Share via"));
                    } else {
                        Toast.makeText(activity, R.string.err_no_app_found, Toast.LENGTH_LONG).show();
                    }
        }
}
于 2021-09-01T06:15:03.527 回答