Friday, January 20, 2012

Upload a video to YouTube using Send intent

This should be a very easy operation, but somehow google managed to make it hard (I wasted a few hours on it),
So, here is a full source code that shows how to do it.
The key point is to create the ContentResolver to create the Uri, if you will not do it, you will be able to send via Gmail, but no upload to YouTube, the uploader will send a error: "file is not found"

Here is the code snippet:
Button share = (Button)findViewById(;
          share.setOnClickListener(new OnClickListener() {
                public void onClick(View v){
                    Log.d("Share Example", "Share button is clicked");
                    String outputFile = "/sdcard/in.mp4";
                    Log.d("Share Example", "outputFileURL: " + outputFile);
                    ContentValues content = new ContentValues(4);
                    content.put(Video.VideoColumns.TITLE, "My Test");
                    System.currentTimeMillis() / 1000);
                    content.put(Video.Media.MIME_TYPE, "video/mp4");
                    content.put(MediaStore.Video.Media.DATA, outputFile);
                    ContentResolver resolver = getContentResolver();
                    Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    Intent intent = new Intent(Intent.ACTION_SEND);
                    intent.putExtra(Intent.EXTRA_STREAM, uri);
                    startActivity(Intent.createChooser(intent, "Share using"));

Here is the full running source code: