tags:

views:

898

answers:

4

I'm writing an Android app where users can upload video to Youtube. I'd like the Youtube tag field to be pre-filled with a tag that I set.

I'd also like the UI to work like this: user clicks on an Upload button, user goes straight to Youtube upload intent (rather than picking from a Chooser), tag field is pre-filled for them.

Is this possible using ACTION_SENDTO?

Currently I have this code, which just launches a Chooser, which really isn't what I want:

    btnUpload.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //uploadToYouTube();
            //videoUpload();
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_SEND);
            intent.setType("video/3gpp");
            intent.putExtra(Intent.EXTRA_STREAM, videoURI);
            try {
                         startActivity(Intent.createChooser(intent,
                                 "Upload video via:"));
            } catch (android.content.ActivityNotFoundException ex) {
                         Toast.makeText(Recorder.this, "No way to share!",
                                 Toast.LENGTH_SHORT).show();
            }
        }
    });
+2  A: 

It's launching a chooser because you're telling it to with Intent.createChooser. You need to specify YouTube's Activity directly.

Upon investigation, it looks like MediaUploader handles YouTube uploading. I looked into it's AndroidManifest.xml and I'm guessing the Intent you want to fire is this:

com.google.android.apps.uploader.UploaderApplication.youtube.YouTubeSettingsActivity

Here is the interesting parts of the AndroidManifest.xml.

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser"
    android:versionName="1.4.13"
    package="com.google.android.apps.uploader">
    <uses-sdk android:minSdkVersion="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser" />
    <application
        android:label="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser"
        android:name=".UploaderApplication"
        android:debuggable="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser">
        <activity
            android:theme="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser"
            android:name=".youtube.YouTubeSettingsActivity"
            android:excludeFromRecents="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser"
            android:configChanges="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser">
            <intent-filter
                android:label="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser"
                android:icon="com.google.android.googleapps.permission.GOOGLE_AUTH.YouTubeUser">
                <data android:mimeType="video/*" />
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.ALTERNATIVE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

You'll notice that the intent-filter for the YouTubeSettingsActivity is receiving the SEND action, so that's likely the intent we want.

As CommonsWare said, however, this is dependent on the phone having YouTube support, and can break. This was taken from my system image of 2.0.1. Make sure you check to see that the intent works before firing it.

Andrew Koester
Which, if it does not exist on a given device, will blow sky high. And, since the YouTube player's Intents are not part of the SDK, if the YouTube player changes their Intents in future editions of Android, you will blow up as well.
CommonsWare
I'm just answering his question as to how to get it to upload directly to YouTube. And as with any intent resolution, you should be checking to make sure it validates before calling it.
Andrew Koester
This code is embedding specific implementation details (the name of the package and class that does YouTube uploading) into your app, which is likely to lead to your app breaking in the future. For example, it is perfectly possible for this functionality to get integrated into the YouTube app, and then the code breaks.Please don't use implementation details you find in manifest files in application code. This is the same as looking at source code of the framework and taking advantage of implementation details you find there.
hackbod
Thanks! Two questions: (i) where did you get that source? I couldn't find it at http://android.git.kernel.org/ (ii) I guess it still isn't possible to pre-supply a tag into the uploader page?
AP257
I got the source by retrieving the APK from my phone's /system directory, which means that it is a private Intent and can't be relied on in the future. This package, like YouTube itself, isn't open source, so it's not in their repository.And as far as I'm aware, no, you can't pre-supply a tag. You might want to look into YouTube's API at http://code.google.com/apis/youtube/2.0/developers_guide_protocol_direct_uploading.html
Andrew Koester
A: 

Personally, I think the current implementation is the right answer -- just because you want YouTube doesn't mean that the user wants YouTube.

CommonsWare
The whole point of my app is that it uploads videos to Youtube with a set tag (it's a video crowdsourcing project)
AP257
...so @CommonsWare, if I am going to do this properly (which I would like to do), what would you recommend? Here's what I need: (1) Videos uploaded with a given Youtube tag and (2) An app that is really simple for the user, and doesn't require them to know that they have to pick Youtube, and then remember and type in a special tag. Surely this must be possible in Android?
AP257
I have no idea, because I have never used any YouTube Web APIs. However, the notion of using a *built-in activity* for this is unlikely to work -- if nothing else, it'll have no concept of your tag. But, perhaps the YouTube Data API Java client library works on Android: http://code.google.com/apis/youtube/code.html#client_libraries
CommonsWare
Thanks. I'll investigate, and post my findings here.
AP257
+2  A: 

Turned out the best way to do it was to upload to my own site via an ordinary POST request, and then upload to YouTube from there, server-side.

AP257
A: 

So, actually this is the undocumented code... BIG THANKS to Andrew Koester for your clues!

          Intent intent = new Intent(Intent.ACTION_SEND,uri);
          intent.setType("video/3gpp");
          intent.setComponent(new ComponentName(
                  "com.google.android.apps.uploader",
                  "com.google.android.apps.uploader.youtube.YouTubeSettingsActivity")

              );
          intent.setFlags(0x3000000); // ParcelFileDescriptor.MODE_READ_WRITE ?!?
          intent.putExtra(Intent.EXTRA_STREAM,uri);

          try {
              startActivity(intent); //              
              // startActivityForResult(intent,23); //only returns OK... how to get URL?!
          } 
          catch (android.content.ActivityNotFoundException ex) {
              Toast.makeText(getApplicationContext(),"No way to share",Toast.LENGTH_SHORT).show();
          }
PLG