开发者

Android Video Upload to Youtube

开发者 https://www.devze.com 2023-04-10 03:31 出处:网络
I am developing an app that uploads a video in the sdcard to youtube and returns the youtube video url after. However, I have not been able to successfully do so.

I am developing an app that uploads a video in the sdcard to youtube and returns the youtube video url after. However, I have not been able to successfully do so.

I have tried with ACTION_SEND:

public void onUploadClick(View v) {
 Intent uploadIntent = new Intent(Intent.ACTION_SEND);
 // In a real app, video would be captured from camera.
 File f = new File("/sdcard/test.mov");
 uploadIntent.setDataAndType(Uri.fromFile(f), "video/quicktime");
 startActivity(Intent.createChooser(uploadIntent, "Upload"));
}

But, oddly, with this implementation I do not have Youtube as an option for the upload. I have Bluetooth, Mail, Dropbox, Facebook, Flickr, Gmail and Handcent. But not Youtube. Any thoughts on this? Besides, is it possible using this method to have the url of the youtube video after uploaded? How?

I have also tried using Youtube API for Java, and again no success. I added all the libraries needed (gdata-client-1.0.jar, gdata-media-1.0.jar and gdata-youtube-2.0,jar). However, when I run it I get a NoClassDefFoundError for com.google.gdata.client.youtube.YouTubeService, despite having the right libs and imports. Here is the code I am using:

YouTubeService service = new YouTubeService(clientID, developer_ke开发者_运维技巧y);
VideoEntry newEntry = new VideoEntry();

YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
mg.setTitle(new MediaTitle());
mg.getTitle().setPlainTextContent("My Test Movie");
mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
mg.setKeywords(new MediaKeywords());
mg.getKeywords().addKeyword("cars");
mg.getKeywords().addKeyword("funny");
mg.setDescription(new MediaDescription());
mg.getDescription().setPlainTextContent("My description");
mg.setPrivate(false);
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "anotherdevtag"));

newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
// alternatively, one could specify just a descriptive string
// newEntry.setLocation("Mountain View, CA");

MediaFileSource ms = new MediaFileSource(new File("file.mov"), "video/quicktime");
newEntry.setMediaSource(ms);

String uploadUrl =
  "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";

VideoEntry createdEntry = service.insert(new URL(uploadUrl), newEntry);

To sum up, my questions are: - What am I doing wrong in both approaches? - Regarding my simple objective of uploading a video to youtube and getting its youtube link after, which method do you recommend? Even if it's neither of those above.

Thank you.

---- Update 04/10 ----

  • After trying with a differente phone I found out that Youtube not showing up as choice to upload the video is my phone's problem (probably due to the ROM). However, even if it does work using intents is not the way to go because I cannot get the URL back and that is essential.
  • After some research it seems that the second method is not possible. The Youtube API for Java does not work in Android.

That being said, I am looking for suggestions on how to upload a video to Youtube in Android. What I really want is to choose a video file, upload it to Youtube and get the youtube URL. Preferably without any interference from the user, who only chooses the file and then the uploading and URL retrieving runs "in the background". Any suggestions? What is the easiest and fastest way to achieve this?

Thanks.


You can use the RESTful YouTube API and perform a resumable upload. Code example here: http://code.google.com/p/ytd-android/. Also, have you tried ACTION_SHARE?


Regarding the first approach: I tried on 3 different phones, all of different brands and different versions of Android and this is how each acted when I clicked share and picked YouTube:

act=android.intent.action.SEND typ=video/mp4 flg=0x3000000 cmp=com.google.android.apps.uploader/.clients.youtube.YouTubeSettingsActivity

act=android.intent.action.SEND typ=video/3gpp flg=0x3000000 cmp=com.google.android.apps.uploader/.clients.youtube.YouTubeSettingsActivity

act=android.intent.action.SEND_MULTIPLE typ=video/* cmp=com.google.android.apps.uploader/.clients.youtube.YouTubeSettingsActivity

So it looks like you're on the right track. I would try changing the mimetype. Start off with video/* maybe just to see if it works.

Oh, as a side note, it might look tempting but it would probably be dangerous to use YouTubeSettingsActivity directly in case they someday change it.

Personally, I like the first approach because it integrates nicely with other apps. Like say your users want to use something other than YouTube, they can choose what they want. I'm not sure about getting back a URL though.

0

精彩评论

暂无评论...
验证码 换一张
取 消