2015-05-16 130 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任何其他解決方案,而壓縮和解壓縮文件? –