Đây là giải pháp hai bước mà tôi đã đưa ra, phần lớn từ thông tin và liên kết được tìm thấy here. Giải pháp này dễ dàng hơn cho tôi để nắm bắt hơn phương thức upload2server() trong một số bài viết SO liên quan. Hy vọng điều này sẽ giúp người khác.
1) Chọn tệp video từ thư viện.
Tạo biến số private static final int SELECT_VIDEO = 3;
- không quan trọng bạn sử dụng số nào, miễn là số bạn sử dụng để kiểm tra sau này. Sau đó, sử dụng ý định chọn video.
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select a Video "), SELECT_VIDEO);
Sử dụng onActivityResult() để bắt đầu phương thức 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) Tới http://hc.apache.org/downloads.cgi, tiến hành download HttpClient mới nhất jar, thêm nó vào dự án của bạn, và tải lên video bằng cách sử dụng phương pháp sau đây:
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()
Một khi bạn đã làm việc đó bạn sẽ có thể muốn đặt nó vào một chủ đề và thêm một hộp thoại tải lên, nhưng điều này sẽ giúp bạn bắt đầu. Làm việc cho tôi sau khi tôi đã thử phương thức upload2Server() không thành công. Điều này cũng sẽ làm việc cho hình ảnh và âm thanh với một số tinh chỉnh nhỏ.
Rất tuyệt! Điều này có thể sử dụng được cho video có kích thước lớn không? Hoặc nó có thể được sử dụng cho các loại tệp khác không? –
@AliBagheriShakib sẽ hoạt động tốt cho video lớn hơn. Có, nó có thể được sử dụng cho các clip âm thanh hoặc các loại tệp khác rất giống nhau. Hãy cho tôi biết nếu bạn có bất kỳ vấn đề. –
Có @Kyle Clegg. Nó làm việc hoàn hảo cho tôi. Vấn đề của tôi được giải quyết bro :) Tôi cũng đăng kinh nghiệm của riêng mình: http://stackoverflow.com/q/23504191/2624611 –