2015-05-16 126 views
0

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

我的代码如下。请给我任何可能的解决方案来解决这个问题。此代码正在工作,但需要太多时间。任何人都可以帮助我改进性能吗?

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

回答

0

您可以使您的数据的.zip文件发送。 通过使zip数据得到压缩(所以尺寸减小)。

以下是有助于您的代码示例。

zip/unzip file in android

+0

您好感谢您的回复....但我不想做一个zip file..is任何其他解决方案,而压缩和解压缩文件? –