Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.Activity;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.net.Uri;
- import android.os.Bundle;
- import android.provider.MediaStore;
- import android.provider.MediaStore.Video;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.gdata.client.*;
- import com.google.gdata.client.youtube.*;
- import com.google.gdata.data.*;
- import com.google.gdata.data.youtube.VideoEntry;
- import com.google.gdata.data.geo.impl.*;
- import com.google.gdata.data.media.*;
- import com.google.gdata.data.media.mediarss.*;
- import com.google.gdata.data.youtube.*;
- import com.google.gdata.data.extensions.*;
- import com.google.gdata.util.*;
- import java.io.ByteArrayOutputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.File;
- import java.net.MalformedURLException;
- import java.net.URL;
- import org.apache.http.auth.AuthenticationException;
- import gr.atc.epart.googlewrapper.youtube.R;
- public class YouTubeActivity extends Activity {
- public static final String developer_key;
- public static final String clientID;
- protected static final int CAMERA_CAPTURE = 0;
- YouTubeService service=null;
- VideoEntry newEntry;
- public static String username;
- public static String password;
- TextView text;
- TextView user;
- TextView passwd;
- EditText enteredName;
- EditText enteredPasswd;
- static{
- /* I know these 4 values : */
- developer_key="";
- clientID="";
- username="";
- password="";
- }
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.login_page);
- text = (TextView) findViewById(R.id.textView1);
- user = (TextView) findViewById(R.id.textView2);
- passwd = (TextView) findViewById(R.id.textView3);
- enteredName = (EditText) findViewById(R.id.editText1);
- enteredPasswd = (EditText) findViewById(R.id.editText2);
- ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- setResult(RESULT_OK);
- finish();
- }
- });
- ((Button) findViewById(R.id.button2)).setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- System.out.println("starting to connect");
- if(enteredName.getText()!=null)
- {
- username = enteredName.getText().toString();
- System.out.println("username: "+username);
- }
- if(enteredPasswd.getText()!=null)
- {
- password = enteredPasswd.getText().toString();
- System.out.println("password: "+password);
- }
- Intent i = new Intent();
- i.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
- startActivityForResult(i, CAMERA_CAPTURE);
- //testGdata();
- //upload();
- }
- });
- Toast.makeText(this, "Authentication Successful", Toast.LENGTH_SHORT).show();
- //testGdata();
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- Uri targetUri;
- if (resultCode == RESULT_OK){
- targetUri = data.getData();
- System.out.println("target"+targetUri);
- byte[] data1 = null;
- //textTargetUri.setText(targetUri.toString());
- Bitmap bitmap;
- try {
- bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
- System.out.println("bitmap"+bitmap);
- //targetImage.setImageBitmap(bitmap);
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
- data1 = baos.toByteArray();
- upload(data1);
- //
- //System.out.println("imagedispaly" +targetImage);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- public boolean authenticate()
- {
- //System.out.println("Start authentication!!");
- boolean auth=false;
- System.out.println("Start authentication!!");
- service = new YouTubeService(clientID, developer_key);
- try
- {
- service.setUserCredentials(username, password);
- System.out.println("service.getCookies(): "+service.getCookies());
- if(service.getCookies()!=null)
- auth=true;
- }
- catch(Exception e)
- {
- System.out.println("Exception: "+e);
- auth=false;
- }
- return auth;
- }
- private void testGdata() {
- YouTubeService service = new YouTubeService(clientID, developer_key);
- VideoEntry newEntry = new VideoEntry();
- YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
- mg.setTitle(new MediaTitle());
- mg.getTitle().setPlainTextContent("Custom title here");
- mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME,
- "Politics"));
- mg.setKeywords(new MediaKeywords());
- mg.getKeywords().addKeyword("myImportantKeyword");
- mg.setDescription(new MediaDescription());
- mg.getDescription().setPlainTextContent("Custom description here");
- mg.setPrivate(false);
- mg.addCategory(new
- MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
- mg.addCategory(new
- MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME,
- "anotherdevtag"));
- newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
- // alternatively, one could specify just a descriptive string
- // newEntry.setLocation("Mountain View, CA");
- //MediaFileSource ms = new MediaFileSource(new File("videofile.3gp"),"video/quicktime");
- //MediaFileSource ms = new MediaFileSource(video, "video/3gpp");
- File n = new File("videofile.3gp");
- MediaFileSource ms = new MediaFileSource(n ,"video/3gp");
- newEntry.setMediaSource(ms);
- String uploadUrl = "http://uploads.gdata.youtube.com/feeds/api/users/default/uploads";
- try {
- VideoEntry createdEntry = service.insert(new URL(uploadUrl),
- newEntry);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (ServiceException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void upload(byte[] data)
- {
- boolean auth;
- try{
- auth=authenticate();
- System.out.println("auth in upload: "+auth);
- if(auth)
- {
- Toast.makeText(YouTubeActivity.this, "Connected to youTube", Toast.LENGTH_LONG);
- finish();
- }
- else
- System.out.println("not authenticated!!!");
- // newEntry = new VideoEntry();
- // YouTubeMediaGroup mg = newEntry.getOrCreateMediaGroup();
- // mg.setTitle(new MediaTitle());
- // mg.getTitle().setPlainTextContent("iconApp");
- // mg.addCategory(new MediaCategory(YouTubeNamespace.CATEGORY_SCHEME, "Autos"));
- // mg.setKeywords(new MediaKeywords());
- // mg.getKeywords().addKeyword("aa");
- // mg.getKeywords().addKeyword("bb");
- // mg.setDescription(new MediaDescription());
- // mg.getDescription().setPlainTextContent("My description");
- // mg.setPrivate(false);
- // mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "mydevtag"));
- // mg.addCategory(new MediaCategory(YouTubeNamespace.DEVELOPER_TAG_SCHEME, "anotherdevtag"));
- //
- // newEntry.setGeoCoordinates(new GeoRssWhere(37.0,-122.0));
- // // alternatively, one could specify just a descriptive string
- // // newEntry.setLocation("Mountain View, CA");
- //
- // URL uploadUrl = new URL("http://gdata.youtube.com/action/GetUploadToken");
- // FormUploadToken token = service.getFormUploadToken(uploadUrl, newEntry);
- //
- // System.out.println("token.getUrl() in upload: "+ token.getUrl());
- // System.out.println("token.getToken() in upload: "+token.getToken());
- }
- catch(Exception e)
- {
- System.out.println("Exception: "+e);
- }
- }
- public void uploadStatus()
- {
- if(newEntry.isDraft()) {
- System.out.println("Video is not live");
- YtPublicationState pubState = newEntry.getPublicationState();
- if(pubState.getState() == YtPublicationState.State.PROCESSING) {
- System.out.println("Video is still being processed.");
- }
- else if(pubState.getState() == YtPublicationState.State.REJECTED) {
- System.out.print("Video has been rejected because: ");
- System.out.println(pubState.getDescription());
- System.out.print("For help visit: ");
- System.out.println(pubState.getHelpUrl());
- }
- else if(pubState.getState() == YtPublicationState.State.FAILED) {
- System.out.print("Video failed uploading because: ");
- System.out.println(pubState.getDescription());
- System.out.print("For help visit: ");
- System.out.println(pubState.getHelpUrl());
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement