我想在发送到服务器之前压缩视频/音频文件,因为它需要太多时间上传。我正在使用带有字符串主体的多部分实体将文件上传到服务器上。
我的代码在下面。请给我任何可能的解决方案。该代码正在工作,但需要太多时间。谁能帮助我提高表现?
try {
if (selectedPath == null || selectedPath == "") {
getActivity().runOnUiThread(new Runnable() {
public void run() {
Alert_Dialogs.custom_alert_dialog(getActivity(),"Please select Video.");
}
});
}
else {
File file = new File(selectedPath);
StringBody title = new StringBody(et_title.getText().toString().trim());
StringBody catId = new StringBody(cat_id);
StringBody user_data = new StringBody(user_id);
FileBody filebodyVideo = new FileBody(file);
CustomMultiPartEntity customMultiPartEntity = new CustomMultiPartEntity(new ProgressListener() {
@Override
public void transferred(long num) {
publishProgress((int) ((num / (float) totalSize) * 100)); }
});
customMultiPartEntity.addPart("title",title );
customMultiPartEntity.addPart("catId",catId );
customMultiPartEntity.addPart("user_id",user_data );
customMultiPartEntity.addPart("qqfile", filebodyVideo);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Application.UploadVideosEndpoint());
// static_url+"WS/videoUpload?api="+static_api_key
totalSize = customMultiPartEntity.getContentLength();
httppost.setEntity(customMultiPartEntity);
HttpResponse response = httpclient.execute( httppost );
HttpEntity resEntity = response.getEntity( );
str_response = EntityUtils.toString(resEntity);
if (resEntity != null) {
resEntity.consumeContent( );
}
httpclient.getConnectionManager( ).shutdown( );
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
您可以将要发送的数据的.zip文件制作。通过制作ZIP数据将被压缩(因此尺寸降低)。
这是有帮助您的代码的工作示例。
Android中的zip/unzip文件