In this tutorial we'll do some steps, like :
- Browse the video in your phone gallery
- Select the video
- Play the video in VideoView before uploading
- Upload the video to youtube
For easy to understanding, i created a method for every function, hope you'll get what is the process look likes
Now let's create the layout, give it name activity_main.xml, and then paste the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:id="@+id/activity_main" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:orientation="vertical" | |
tools:context="com.putuguna.uploadvideotoyoutube.MainActivity"> | |
<TextView | |
android:gravity="center" | |
android:layout_width="match_parent" | |
android:textStyle="bold" | |
android:textColor="@android:color/black" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10dp" | |
android:text="@string/page_description" /> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:orientation="horizontal" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10dp"> | |
<Button | |
android:layout_weight="1" | |
android:text="@string/label_select_video" | |
android:id="@+id/btn_select_video" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" /> | |
<Button | |
android:layout_weight="1" | |
android:id="@+id/btn_upload" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/label_upload_video"/> | |
</LinearLayout> | |
<VideoView | |
android:id="@+id/video_view" | |
android:layout_weight="1" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> |
In your java class, there are some method that will called during the process, there are :
1. Select the video from gallery and displaying on the VideoView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* this method used to select video from gallery | |
*/ | |
public void selectVideoFromGallery() { | |
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { | |
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); | |
} else { | |
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI); | |
} | |
intent.setType("video/*"); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
} | |
/** | |
* this method used to set video to VideoView | |
* @param uri | |
*/ | |
private void setVideoIntoVideoView(Uri uri){ | |
MediaController mc = new MediaController(this); | |
mc.setAnchorView(videoView); | |
mc.setMediaPlayer(videoView); | |
videoView.setMediaController(mc); | |
videoView.setVideoURI(uri); | |
videoView.start(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if(requestCode==TAKE_VIDEO_INTEGER){ | |
if(resultCode==RESULT_OK){ | |
if(data.getData()!=null){ | |
//generate video path | |
pathSelected = generatePath(data.getData(),this);//this method for generate the video path from gallery | |
//set video to VideoView | |
setVideoIntoVideoView(data.getData()); | |
btnUpload.setEnabled(true); | |
btnUpload.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
shareVideo(pathSelected); //method for uploading the video | |
} | |
}); | |
}else{ | |
Toast.makeText(this, R.string.msg_failed_to_select_video, Toast.LENGTH_SHORT).show(); | |
} | |
}else{ | |
Toast.makeText(this, R.string.msg_canceling, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} |
2. Generate the video path from gallery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* this method used to get the video's path | |
* @param uri | |
* @param context | |
* @return | |
*/ | |
public String generatePath(Uri uri,Context context) { | |
String filePath = null; | |
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; | |
if(isKitKat){ | |
filePath = generateFromKitkat(uri,context); | |
} | |
if(filePath != null){ | |
return filePath; | |
} | |
Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.MediaColumns.DATA }, null, null, null); | |
if (cursor != null) { | |
if (cursor.moveToFirst()) { | |
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); | |
filePath = cursor.getString(columnIndex); | |
} | |
cursor.close(); | |
} | |
return filePath == null ? uri.getPath() : filePath; | |
} | |
/** | |
* this method use to get the video's path on TargetAPI 19 | |
* @param uri | |
* @param context | |
* @return | |
*/ | |
@TargetApi(19) | |
private String generateFromKitkat(Uri uri,Context context){ | |
String filePath = null; | |
if(DocumentsContract.isDocumentUri(context, uri)){ | |
String wholeID = DocumentsContract.getDocumentId(uri); | |
String id = wholeID.split(":")[1]; | |
String[] column = { MediaStore.Video.Media.DATA }; | |
String sel = MediaStore.Video.Media._ID + "=?"; | |
Cursor cursor = context.getContentResolver(). | |
query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, | |
column, sel, new String[]{ id }, null); | |
int columnIndex = cursor.getColumnIndex(column[0]); | |
if (cursor.moveToFirst()) { | |
filePath = cursor.getString(columnIndex); | |
} | |
cursor.close(); | |
} | |
return filePath; | |
} |
There are the method that i created in this project, hope your understand the process from the begin until the end (Video uploaded).
Here the full code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.uploadvideotoyoutube; | |
import android.annotation.TargetApi; | |
import android.content.ContentResolver; | |
import android.content.ContentValues; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.media.ThumbnailUtils; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Handler; | |
import android.provider.DocumentsContract; | |
import android.provider.MediaStore; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.ImageView; | |
import android.widget.MediaController; | |
import android.widget.Toast; | |
import android.widget.VideoView; | |
import com.google.android.youtube.player.YouTubeBaseActivity; | |
import com.google.android.youtube.player.YouTubeInitializationResult; | |
import com.google.android.youtube.player.YouTubePlayer; | |
public class MainActivity extends AppCompatActivity { | |
public static final int TAKE_VIDEO_INTEGER = 10011; | |
private Button btnUpload; | |
private Button btnSelectVideo; | |
private VideoView videoView; | |
private Intent intent; | |
private String pathSelected; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
btnUpload = (Button) findViewById(R.id.btn_upload); | |
btnSelectVideo = (Button) findViewById(R.id.btn_select_video); | |
videoView = (VideoView) findViewById(R.id.video_view); | |
btnUpload.setEnabled(false); | |
btnSelectVideo.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
selectVideoFromGallery(); | |
startActivityForResult(intent,TAKE_VIDEO_INTEGER); | |
} | |
}); | |
} | |
/** | |
* this method used to get the video's path | |
* @param uri | |
* @param context | |
* @return | |
*/ | |
public String generatePath(Uri uri,Context context) { | |
String filePath = null; | |
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; | |
if(isKitKat){ | |
filePath = generateFromKitkat(uri,context); | |
} | |
if(filePath != null){ | |
return filePath; | |
} | |
Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.MediaColumns.DATA }, null, null, null); | |
if (cursor != null) { | |
if (cursor.moveToFirst()) { | |
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); | |
filePath = cursor.getString(columnIndex); | |
} | |
cursor.close(); | |
} | |
return filePath == null ? uri.getPath() : filePath; | |
} | |
/** | |
* this method use to get the video's path on TargetAPI 19 | |
* @param uri | |
* @param context | |
* @return | |
*/ | |
@TargetApi(19) | |
private String generateFromKitkat(Uri uri,Context context){ | |
String filePath = null; | |
if(DocumentsContract.isDocumentUri(context, uri)){ | |
String wholeID = DocumentsContract.getDocumentId(uri); | |
String id = wholeID.split(":")[1]; | |
String[] column = { MediaStore.Video.Media.DATA }; | |
String sel = MediaStore.Video.Media._ID + "=?"; | |
Cursor cursor = context.getContentResolver(). | |
query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, | |
column, sel, new String[]{ id }, null); | |
int columnIndex = cursor.getColumnIndex(column[0]); | |
if (cursor.moveToFirst()) { | |
filePath = cursor.getString(columnIndex); | |
} | |
cursor.close(); | |
} | |
return filePath; | |
} | |
/** | |
* this method used to select video from gallery | |
*/ | |
public void selectVideoFromGallery() { | |
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { | |
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI); | |
} else { | |
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI); | |
} | |
intent.setType("video/*"); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
} | |
/** | |
* this method used to set video to VideoView | |
* @param uri | |
*/ | |
private void setVideoIntoVideoView(Uri uri){ | |
MediaController mc = new MediaController(this); | |
mc.setAnchorView(videoView); | |
mc.setMediaPlayer(videoView); | |
videoView.setMediaController(mc); | |
videoView.setVideoURI(uri); | |
videoView.start(); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if(requestCode==TAKE_VIDEO_INTEGER){ | |
if(resultCode==RESULT_OK){ | |
if(data.getData()!=null){ | |
pathSelected = generatePath(data.getData(),this); | |
//set video to VideoView | |
setVideoIntoVideoView(data.getData()); | |
btnUpload.setEnabled(true); | |
btnUpload.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
//addVideo(pathSelected); | |
shareVideo(pathSelected); | |
} | |
}); | |
}else{ | |
Toast.makeText(this, R.string.msg_failed_to_select_video, Toast.LENGTH_SHORT).show(); | |
} | |
}else{ | |
Toast.makeText(this, R.string.msg_canceling, Toast.LENGTH_SHORT).show(); | |
} | |
} | |
} | |
/** | |
* this method used to get content uri from path | |
* @param ctx | |
* @param filePath | |
* @return | |
*/ | |
public static String getVideoContentUriFromFilePath(Context ctx, String filePath) { | |
ContentResolver contentResolver = ctx.getContentResolver(); | |
String videoUriStr = null; | |
long videoId = -1; | |
Log.d("first log","Loading file " + filePath); | |
// This returns us content://media/external/videos/media (or something like that) | |
// I pass in "external" because that's the MediaStore's name for the external | |
// storage on my device (the other possibility is "internal") | |
Uri videosUri = MediaStore.Video.Media.getContentUri("external"); | |
Log.d("second log","videosUri = " + videosUri.toString()); | |
String[] projection = {MediaStore.Video.VideoColumns._ID}; | |
// TODO This will break if we have no matching item in the MediaStore. | |
Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null); | |
cursor.moveToFirst(); | |
int columnIndex = cursor.getColumnIndex(projection[0]); | |
videoId = cursor.getLong(columnIndex); | |
Log.d("third log","Video ID is " + videoId); | |
cursor.close(); | |
if (videoId != -1 ) videoUriStr = videosUri.toString() + "/" + videoId; | |
return videoUriStr; | |
} | |
/** | |
* this method used to share video to youtube | |
* @param path | |
*/ | |
public void shareVideo(final String path) { | |
new Handler().post(new Runnable() { | |
@Override | |
public void run() { | |
String newPath = getVideoContentUriFromFilePath(MainActivity.this, path); | |
Intent intent = new Intent(Intent.ACTION_SEND); | |
intent.putExtra(android.content.Intent.EXTRA_TITLE, getString(R.string.label_video_title)); | |
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.label_video_description)); | |
intent.setType("video/*"); | |
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(newPath)); | |
try { | |
startActivity(Intent.createChooser(intent, getString(R.string.label_uploading_via))); | |
} catch (android.content.ActivityNotFoundException ex) { | |
} | |
} | |
}); | |
} | |
} |
EmoticonEmoticon