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 开发者_如何学JAVAto 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();
}
}
});
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.
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.
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(); }
Personally, I think the current implementation is the right answer -- just because you want YouTube doesn't mean that the user wants YouTube.
精彩评论