ここでは、私が思いついた2つのステップの解決方法を紹介します。情報とリンクは主にhereです。このソリューションは、関連するSOの投稿のうちのいくつかでupload2server()メソッドよりも把握が容易でした。これが他の人に役立つことを願っています
1)ギャラリーからビデオファイルを選択します。
変数を作成します。private static final int SELECT_VIDEO = 3;
- 後でチェックするものであれば、使用する数字は関係ありません。次に、インテントを使用してビデオを選択します。
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO);
uploadVideo()メソッドを起動するには、onActivityResult()を使用します。あなたはそれがあなたの作業をしたら、おそらくます
private void uploadVideo(String videoPath) throws ParseException, IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(YOUR_URL);
FileBody filebodyVideo = new FileBody(new File(videoPath));
StringBody title = new StringBody("Filename: " + videoPath);
StringBody description = new StringBody("This is a description of the video");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
// DEBUG
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
} // end if
if (resEntity != null) {
resEntity.consumeContent();
} // end if
httpclient.getConnectionManager().shutdown();
} // end of uploadVideo()
:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_VIDEO) {
System.out.println("SELECT_VIDEO");
Uri selectedVideoUri = data.getData();
selectedPath = getPath(selectedVideoUri);
System.out.println("SELECT_VIDEO Path : " + selectedPath);
uploadVideo(selectedPath);
}
}
}
private String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.SIZE, MediaStore.Video.Media.DURATION};
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;
}
2)あなたのプロジェクトに追加し、最新のHttpClient jarファイルをダウンロードし、http://hc.apache.org/downloads.cgiに移動し、以下の方法を使用してビデオをアップロードそれをスレッドに入れてアップロードダイアログを追加したいと思っていますが、これはあなたを始めます。 upload2Server()メソッドをうまく試してみると、私のために働いています。これは、マイナーな調整を加えて画像やオーディオにも機能します。
いいですよ!これは大容量ビデオに使用できますか?または、別のファイルタイプに使用できますか? –
@AliBagheriShakibは、より大きな動画では問題なく動作します。はい、オーディオクリップやその他のファイルタイプに非常によく似ています。何か問題がある場合は教えてください。 –
はい@Kyle Cleggです。それは私のために完璧に働いた。私の問題は仲間を解決:)私自身の経験を投稿する:http://stackoverflow.com/q/23504191/2624611 –