分享意图不适用于将视频上传到youtube

问题描述 投票:3回答:6

我正在尝试共享正在创建并存储在已获取路径的外部SD卡上的视频。

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getAbsolutePath()

我使用SEND_INTENT如下:

Intent shareIntent = new Intent(Intent.ACTION_SEND);                         
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
shareIntent.setType("video/mp4");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Subject");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,"My Text");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(video_path));
startActivityForResult(Intent.createChooser(shareIntent, "Share Your Video"),SHARE_INTENT);

问题:当我通过gmail分享时,它会显示我附加视频的撰写窗口。但是没有显示视频的大小,当您发送或取消窗口时,gmail将在内容解析器上与输入流NPE崩溃。

对于youtube,它表示您无法从云服务上传视频,我的视频显然位于设备上。

在Facebook的情况下,它被默默地丢弃。这适用于wassup。 :-)

任何想法如何让这个工作?

编辑:

视频路径:/storage/emulated/0/Movies/MyFolder/my-video_1378253389208.mp4

UPDATE

通过添加file:///后缀,gmail和facebook工作正常。 Youtube仍然抱怨“视频无​​法从云服务上传”。

android facebook video android-intent youtube
6个回答
4
投票

Youtube失败和其他人工作的原因是因为它检查它是否是媒体文件。它只是认为它是一个媒体文件,如果它已被扫描。运行下面的代码,它应该工作。它也将出现在画廊中。如何上传我不知道的临时文件。

void publishScan() {

    //mVidFnam: "/mnt/sdcard/DCIM/foo/vid_20131001_123738.3gp" (or such)
    MediaScannerConnection.scanFile(this, new String[] { mVidFnam }, null,
            new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    Log.d(TAG, "onScanCompleted uri " + uri);


                }
            });
}

2
投票

根据Johan vdH的回答,以下代码适用于许多共享应用程序,包括Youtube,Facebook,WhatsApp等。

路径应该是视频文件的绝对路径。例如。 “/mnt/sdcard/DCIM/foo/vid_20131001_123738.3gp”

public void shareVideo(final String title, String path) {
        MediaScannerConnection.scanFile(getActivity(), new String[] { path },
                null, new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String path, Uri uri) {
                        Intent shareIntent = new Intent(
                                android.content.Intent.ACTION_SEND);
                        shareIntent.setType("video/*");
                        shareIntent.putExtra(
                                android.content.Intent.EXTRA_SUBJECT, title);
                        shareIntent.putExtra(
                                android.content.Intent.EXTRA_TITLE, title);
                        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
                        shareIntent
                                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                        context.startActivity(Intent.createChooser(shareIntent,
                                getString(R.string.str_share_this_video)));

                    }
                });
    }

1
投票

除了约翰vdH的回答,Uri用于

shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

必须是从中获得的

public void onScanCompleted(String path, Uri uri)

获得Uri

Uri  uri = Uri.fromFile(new File(video_path));

不管用。 Youtube似乎喜欢内容://但不是文件://


1
投票

在youtube上分享视频的最佳和最简单的方法,使用Provider在Api> = 24的设备中获取视频Uri

 private void VideoShareOnYoutube(String videoPath) {
        try {
           String csYoutubePackage = "com.google.android.youtube";
            Intent intent = getPackageManager().getLaunchIntentForPackage(csYoutubePackage);

            if (intent != null && !videoPath.isEmpty()) {

                Intent share = new Intent(Intent.ACTION_SEND);
                share.setPackage(csYoutubePackage);
                Uri contentUri;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    contentUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider",  new File(videoPath));

                }
                else
                {
                    contentUri = Uri.fromFile(new File(videoPath));

                }
                share.setType("video/*");
                share.putExtra(Intent.EXTRA_STREAM, contentUri);

                startActivity(share);

            } else {
                Common.showToast("Youtube app not installed!", activty);
            }
        } catch (Exception e) {
            Log.e(TAG, "Youtubeexeption() :" + e.getMessage());
        }
    }

0
投票

使用此代码,您可以尝试。

 public void makeVideo(String nameVideo){
    String type = "video/*";
   // /storage/emulated/0/nameVideo.mp4
   // if you have other path is necessary you change the path

    String mediaPath = Environment.getExternalStorageDirectory() + File.separator + nameVideo;

    createIntent(type, mediaPath);
}

private void createIntent(String type,String mediaPath){

    // Create the new Intent using the 'Send' action.
    Intent share = new Intent(Intent.ACTION_SEND);

    // Set the MIME type
    share.setType(type);

    // Create the URI from the media
    File media = new File(mediaPath);
    Uri uri = Uri.fromFile(media);

    // Add the URI to the Intent.
    share.putExtra(Intent.EXTRA_STREAM, uri);

    // Broadcast the Intent.
    startActivity(Intent.createChooser(share, "Share to"));
}

更多信息https://instagram.com/developer/mobile-sharing/android-intents/


0
投票

只是一个抬头,因为当我试图通过Intent Chooser与Facebook分享视频时,我发现这一点与Gmail和其他一些意图一起运行良好 - 如果有空格或其他字符被编码为%20空间,例如Facebook如果您尝试播放,则无法找到该视频。

我正在使用视频 - 2016年4月27日 - 16:59 pm.mp4,空格和冒号被替换,打破了Facebook内部的链接。在Facebook上撰写共享时,视频的缩略图会显示,所以我认为没关系,但是如果你点击播放它,就会说无法找到/播放该文件。

没有这样的问题,Gmail发送,并且一旦从电子邮件中检索就可以播放。

© www.soinside.com 2019 - 2024. All rights reserved.