如何在Android中将视频上传到youtube?

问题描述 投票:13回答:7

我正在创建一个记录视频并将其上传到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?"));
android video upload youtube-api share
7个回答
17
投票

试试这个代码。

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()方法中并获取结果。将EXTRA_STREAM中的值作为URI而不是文件传递。


6
投票

我没有足够的声誉来评论,但我失去了一个小时左右试图让user2126788的代码工作,因为我忘了在意图上设置类型。

sharingIntent.setType("video/*");

1
投票

请在android清单中添加此行

     <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>

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:")); 

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

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