2013-09-30 38 views
-4

我已經創建了一個Android應用程序。這需要從我的asp.net web api下載PDF,音頻和視頻文件到我的android應用程序。但我沒有一個關於我的web api代碼會是什麼樣的想法,以及我的android下載的pdf,音頻文件和視頻文件的代碼如何。 謝謝你的細讀。從網絡服務器通過Android下載PDF文件,音頻和視頻文件

+0

叫它請更多的谷歌前/研究發問。你的問題就像「嘿,我需要一個Android應用程序和服務器,你能爲我寫嗎?」 – sswierczek

+0

非常感謝@Sebastian。評論指出。我目前正在研究這個問題。我只是在我的項目中將時間線上的這一點粘貼到了後面。無論如何,如果我違反了發佈規則,我真的很抱歉。我明白,每個開發者都必須願意做研究,才能在行業中脫穎而出。非常感謝。充分注意到下一篇文章 – user2754532

+0

我已經發布http下載代碼,但作爲@塞巴斯蒂安說你必須搜索更多 –

回答

1

您可以自定義,並利用這一點,你需要激活鎖定的權限,以防止中斷

@SuppressLint("Wakelock") 
public class DownloadTask extends AsyncTask<String, Integer, String> 
{ 

    private Context context; 

    public DownloadTask(Context context ,Dialog dialog, ProgressBar progressBar ,TextView progressTextView , String destinationPath ,String fileName , JSONObject jObject) 
    { 
     this.context = context; 
    } 

    @SuppressWarnings("resource") 
    @Override 
    protected String doInBackground(String... sUrl) 
    { 
     String directory = sUrl[0]; 
     String fileName = sUrl[1]; 


     //prevent CPU from going off if the user presses the power button during download 
     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName()); 
     wl.acquire(); 

     //download 
     try 
     { 
      new File(directory).mkdirs(); 
      InputStream input = null; 
      OutputStream output = null; 
      HttpURLConnection connection = null; 
      try 
      { 
       //connect to url 
       URL url = new URL(sUrl[2]); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.connect(); 


       // check for http_ok (200) 
       if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) 
        return "Server returned HTTP " 
          + connection.getResponseCode() + " " 
          + connection.getResponseMessage(); 


       int fileLength = connection.getContentLength(); 
       // download the file 
       input = connection.getInputStream(); 
       output = new FileOutputStream(directory+"/"+fileName+".mp3");//change extension 

       //copying 
       byte data[] = new byte[4096]; 
       long total = 0; 
       int count; 
       while ((count = input.read(data)) != -1) 
       { 
        // allow canceling 
        if (isCancelled()) 
        { 
         new File(directory+"/"+fileName+".mp3").delete();//delete partially downloaded file 
         return null; 
        } 
        total += count; 
        if (fileLength > 0) //publish progress only if total length is known 
         publishProgress((int)(total/1024) , fileLength/1024);//(int) (total * 100/fileLength)); 
        output.write(data, 0, count); 
       } 
      } 
      catch (Exception e) 
      { 
       return e.toString(); 
      } 
      finally //closing streams and connection 
      { 
       try 
       { 
        if (output != null) 
         output.close(); 
        if (input != null) 
         input.close(); 
       } 
       catch (IOException ignored) 
       { 
       } 

       if (connection != null) 
        connection.disconnect(); 
      } 
     } 
     finally 
     { 
      wl.release(); // release the lock screen 
     } 
     return null; 
    } 

    @Override // onPreExecute and onProgressUpdate run on ui thread so you can update ui from here 
    protected void onPreExecute() 
    { 
     super.onPreExecute(); 
    } 

    @Override 
    protected void onProgressUpdate(Integer... progress) 
    { 
     super.onProgressUpdate(progress); 
    } 

    @Override 
    protected void onPostExecute(String result) 
    { 
     if (result != null) 
      Toast.makeText(context, "Download error: " + result,Toast.LENGTH_SHORT).show(); 
     else 
     { 
       Toast.makeText(context, " download complete ",Toast.LENGTH_SHORT).show(); 

     } 
    } 
} 

,然後從主

new DownloadTask(this).execute(dirPath , fileName , urlToDownload); 
+0

非常感謝。我會嘗試你的建議和我的研究,並可能編輯問題。如果我遇到任何問題。非常感謝。 – user2754532

相關問題