0

I want to use my application to upload videos on YouTube. I'm using this code:

uploadIntent.setDataAndType(Uri.fromFile(f), "video/quicktime");
startActivity(Intent.createChooser(uploadIntent, "Upload"));

but it also shows me options for bluetooth, gmail etc. when I want to it to display only the YouTube option. How can I display only the YouTube option?

kabuko
  • 36,028
  • 10
  • 80
  • 93
Araib karim
  • 421
  • 4
  • 16
  • 3
    possible duplicate of [android youtube upload using intent](http://stackoverflow.com/questions/5884092/android-youtube-upload-using-intent) – kabuko Jun 12 '12 at 22:55
  • its a different question from the above. Found a similar question on Gmail but im looking for only youtube option thanx. – Araib karim Jun 12 '12 at 22:58
  • Both questions deal with trying to upload videos on YouTube on Android via an `Intent`. I fail to see the difference. Please elaborate if you are looking for something different. – kabuko Jun 12 '12 at 23:00
  • [link](http://stackoverflow.com/questions/4883199/using-android-intent-action-send-for-sending-mail) – Araib karim Jun 12 '12 at 23:05
  • 1
    What if the user does not want to upload to YouTube? What if the user would like to upload to another video sharing service? Or a file sharing service like Dropbox? Why do you feel that you need to control where the user is allowed to upload this video? http://commonsware.com/blog/2011/06/28/share-where-the-user-wants.html – CommonsWare Jun 12 '12 at 23:09
  • That's why they use createChooser, so the user can chose where to upload the file. Are you sure the YouTube app is installed on your device? – Christine Jun 12 '12 at 23:51
  • @CommonsWare While in principle I agree with you, I could easily see certain situations come up where you have to restrict. For example, if the app is (or includes) some sort of YouTube client which also displays videos from YouTube. It would then be somewhat nonsensical to upload elsewhere from this app. – kabuko Jun 12 '12 at 23:52
  • @kabuko: "if the app is (or includes) some sort of YouTube client", then they should be using the YouTube Data API to upload the video. – CommonsWare Jun 13 '12 at 00:09
  • @CommonsWare I wouldn't disagree with that. In fact, if the OP really wants to only integrate with YouTube, that should probably be something to consider (using the API). – kabuko Jun 13 '12 at 00:38

1 Answers1

2

I had misunderstood what you were asking. It seems that you have the upload working but want to restrict it to only YouTube. To achieve this, you should not use Intent.createChooser since there's no point in choosing if you only want to display one option. Instead, you can use Intent.setClassName to specify the package and class for YouTube.

You can discover the correct values by simply examining the Intents passed back in your current code when you select YouTube. You'll want to set the return value of Intent.createChooser from PackageManger.queryIntentActivites to a local variable, set a breakpoint in the line after and examine the contents of the YouTube Intent when it breaks.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • First, the return value of `Intent.createChooser()` will not be an `Intent` for the chosen app, simply because the chooser will not have appeared by this point. Second, nobody with any sense should be using `setClassName()` to reference third-party code, as whatever values you use are one refactoring away from being wrong. – CommonsWare Jun 13 '12 at 00:12
  • 1
    @CommonsWare Ah, you're right. OK, forget using `Intent.createChooser()`. Maybe use [`PackageManager.queryIntentActivities`](http://developer.android.com/reference/android/content/pm/PackageManager.html#queryIntentActivities%28android.content.Intent,%20int%29) instead. As far as your second point... yes it's probably a bad idea, but if the developer insists on using an `Intent` to force an upload with only YouTube that's pretty much the only choice. You could protect yourself a bit by using `queryIntentActivities` and falling back to just using the chooser if you like. – kabuko Jun 13 '12 at 00:32