Project demo [You must be registered and logged in to see this link.]
- Code:
package com.example.uploadvideobrightcove;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class UpLoadActivity extends Activity {
TextView tvResult;
public ProgressDialog dialog;
public static final int progress_bar_type = 0;
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_up_load);
Button uploadButton = (Button) findViewById(R.id.btUpLoad);
tvResult=(TextView)findViewById(R.id.txtResult);
uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select a Video "), 2);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
System.out.println("SELECT_VIDEO");
Uri selectedImageUri = data.getData();
String selectedPath = getPath(selectedImageUri);
Toast.makeText(getApplicationContext(), selectedPath,
Toast.LENGTH_LONG).show();
Log.d("link duong dan", "gg" + selectedPath);
new UploadAsyncTask().execute(selectedPath);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.up_load, menu);
return true;
}
private String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA,
MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
cursor.moveToFirst();
String filePath = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
int fileSize = cursor.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));
long duration = TimeUnit.MILLISECONDS.toSeconds(cursor.getInt(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
// some extra potentially useful data to help with filtering if
// necessary
System.out.println("size: " + fileSize);
System.out.println("path: " + filePath);
System.out.println("duration: " + duration);
return filePath;
}
public class UploadAsyncTask extends AsyncTask<String, String, String> {
ProgressDialog PD;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(UpLoadActivity.this);
dialog.setMessage("Uploading...");
dialog.setIndeterminate(false);
dialog.setTitle("UPLOADING");
// dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.show();
}
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://api.brightcove.com/services/post");
try {
MultipartEntity entity;
File f;
FileBody fb;
entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
/*address = Environment.getExternalStorageDirectory()
+ "/testVideo1.mp4";*/
String pictureName = "pitch_active_video_image";
f = new File(params[0]);
fb = new FileBody(f, "application/octect-stream");
String fileName = f.getName();
String jon2 = "{\"params\":{\"video\":{\"name\":\""+fileName+"\",\"shortDescription\":\"dddddddddddd\",\"startDate\":1398043979000,\"endDate\":null},\"encode_to\":\"MP4\",\"create_multiple_renditions\":\"False\",\"token\":\"ZY4Ls9Hq6LCBgleGDTaFRDLWWBC8uoXQHkhGuDebKvjFPjHb3iT-4g..\"},\"method\":\"create_video\"}";
Log.d("mp4", "test" + fileName + params[0]);
Log.d("json", ""+jon2);
String jsonName = "JSON-RPC";
entity.addPart(jsonName, new StringBody(jon2));
try {
entity.addPart(fileName, fb);
} catch (Exception e) {
// TODO: handle exception
}
//send data
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer stringBuffer = new StringBuffer("");
String line = "";
String LineSeparator = System.getProperty("line.separator");
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + LineSeparator);
}
bufferedReader.close();
Log.d("jsonreponse",""+bufferedReader.toString() );
//get result
return stringBuffer.toString();
} catch (ClientProtocolException e) {
return e.toString();
} catch (IOException e) {
return e.toString();
}
}
protected void onPostExecute(String result) {
Log.d("result", result);
dialog.dismiss();
tvResult.setText(result);
}
}
}