Android在发送到服务器之前压缩音频/视频

时间:2015-05-16 05:25:54

标签: android androidhttpclient multipartentity

我想在发送到服务器之前压缩视频/音频文件,因为上传需要太多时间。我正在使用带有字符串主体的多部分实体在服务器上上传文件。

我的代码如下。请给我任何可能的解决方案。此代码正常运行,但需要花费太多时间。谁能帮助我改善表现?

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();
}

1 个答案:

答案 0 :(得分:0)

您可以发送要发送的数据的.zip文件。 通过使zip数据被压缩(因此缩小了大小)。

以下是带有帮助您的代码的工作示例。

zip/unzip file in android