如何在安卓手机上上传视频到YouTube?

14

我正在开发一款应用程序,可以记录视频并上传到YouTube和其他社交网站。

我使用Droid共享功能进行上传,并且它运行良好。

在电子邮件、Facebook、Skype等应用中,上传工作完美,但当我选择YouTube时,它无法上传我的视频。

这是我用于视频分享的代码。

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"SUBJECT_NAME");
sharingIntent.setType("video/*");
File newFile = new File(video_path);
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.fromFile(newFile));
startActivity(Intent.createChooser(sharingIntent,"Where you want to share?"));

4
我也遇到了这个问题,我尝试了很多方法但都没有找到完美的解决方案。 - user2126788
8
我尝试找到解决方案,有时候取得了不错的结果,但对于与谷歌API一起使用来分享YouTube视频的Droid Share,我还没有找到解决方案。 - Mr.Sandy
2
我非常喜欢提出这个问题,因为如果这个问题得到完美的答案,那么我就需要使用它。 - user2126788
1
尝试按照此处指定的方式构建URI:http://stackoverflow.com/questions/5884092/android-youtube-upload-using-intent/6482526#comment10452237_6482526 - Matias Molinas
我可以得到分享视频的链接吗? - Konstantin Konopko
8个回答

18

试试这段代码。

ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, "video_path");
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.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"share:")); 

将以下代码添加到您的分享按钮的onClick()方法中,然后获取结果。 将值作为URI而不是文件传递给EXTRA_STREAM。

2
@Sandip Chaniyara,感谢您使用这段代码...我也已经实现了它。 - user2126788
很棒,谢谢...对于内部存储,您可以使用INTERNAL_CONTENT_URI。 - dangalg
我只收到“没有应用程序可以执行此操作”,为什么? - Patrick

7

我没有足够的声望来发表评论,但是我花了一个小时左右尝试让user2126788的代码工作,因为我忘记在意图上设置类型。

sharingIntent.setType("video/*");

1
请在Android清单文件中添加此行:

please add this line in android manifest

     <intent-filter>
     <action android:name="android.intent.action.SEND" />
     <category android:name="android.intent.category.DEFAULT" />              
     <data android:host="www.youtube.com" android:mimeType="text/*" /> 
     </intent-filter>

4
把它放到用于视频分享的Activity中。只需将其放入清单文件中,而不更改其他内容。 - Mr.Sandy
4
我放置了它,但没有起作用,出现了相同的问题,它会重定向到活动。这段代码还需要做其他更改吗? - Mr.Sandy

1

经过几个小时的尝试,我终于找到了如何在Facebook、YouTube、Instagram和WhatsApp上上传和分享视频的方法。这是适用于我自己的代码。 将应用程序中录制的视频上传到社交媒体应用程序

ContentValues content = new ContentValues(4);
            content.put(Video.VideoColumns.DATE_ADDED,
            System.currentTimeMillis() / 1000);
            content.put(Video.Media.MIME_TYPE, "video/mp4");
            content.put(MediaStore.Video.Media.DATA, "your_path_to_video");
            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:")); 

1
很棒的回答@Tarek Mowafy。但是我能否在YouTube上传页面中获取标题和描述为空的内容。 - David
@David,非常抱歉回复晚了。再次向您道歉,但我无法理解您的问题。当您上传视频至YouTube时,您必须选择一个标题和描述,是吗? - Tarek Mowafy

0

这段代码对我来说很好用。

我尝试实现并获得最佳结果。

所以,如果有人遇到这种问题,请尝试使用这段代码。

我相信它也会完美地为您工作。

ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, "path_of_video");
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.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title_text");
//sharingIntent.setType("video/*");
//File newFile = new File(path_var);
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Where you want to share?")); 

这段代码放在你的分享按钮的onClick()方法中并得到结果。


0

这段代码对我来说运行得很好。

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title_text");
//sharingIntent.setType("video/*");
File newFile = new File(path_var);
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Where you want to share?")); 

0

另一个好的解决方案是在不离开应用程序的情况下实现自己的上传机制。 您可以使用Google Java和Android库以及YouTube java库。

以下是一个完整的应用程序,利用此功能:https://github.com/youtube/yt-direct-lite-android


0
        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, path);
        ContentResolver resolver = getActivity().getContentResolver();
        Uri uri1 =  Uri.fromFile(new File(path)); 

        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("video/*");
        sharingIntent.setPackage("com.google.android.youtube");
        sharingIntent.putExtra(Intent.EXTRA_TITLE, "Title");
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Desc");
        sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri1);
        startActivity(Intent.createChooser(sharingIntent, "Share to"));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接