2013-03-14 149 views
-1

我希望我的應用從網址下載視頻。 現在我想寫下載文件到我的SD卡。Android手機在下載視頻(mp4)文件時崩潰

我嘗試了一些不同的腳本,我沒有收到android.os.NetworkOnMainThreadException異常。但我的應用程序崩潰。 Download a file programatically on Android What is best way to download files from net programatically in android?

有沒有人知道如何創建一個工作方法? 要解決這個例外,它必須是一個異步任務。

public static void downloadFile(String url, File outputFile) { 
     try { 
       URL u = new URL(url); 
       URLConnection conn = u.openConnection(); 
       int contentLength = conn.getContentLength(); 

       DataInputStream stream = new DataInputStream(u.openStream()); 

       byte[] buffer = new byte[contentLength]; 
       stream.readFully(buffer); 
       stream.close(); 

       DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile)); 
       fos.write(buffer); 
       fos.flush(); 
       fos.close(); 
      } catch(Exception e) { 
       Log.e("theple", "" + e); 
      } 
    } 

日誌:

03-14 12:09:46.535: E/theple(6987): android.os.NetworkOnMainThreadException 
+1

在這裏粘貼你的日誌。 – 2013-03-14 11:30:50

+0

它並不是一個異步任務。所有你需要的是在後臺線程中運行它。 AsyncTask只是實現這一點的一種方式。 – 2013-03-14 11:35:36

+0

@AleksG你有一個例子讓我得到這個工作? – 2013-03-14 11:51:49

回答

0

我做這個工作,THX的幫助反正。

我的代碼:

public class FileDownloader extends AsyncTask<String, Integer, String> 
{ 

    @Override 
    protected String doInBackground(String... params) 
    { 
     try { 
       URL u = new URL(params[0]); 
       URLConnection conn = u.openConnection(); 
       int contentLength = conn.getContentLength(); 

       DataInputStream stream = new DataInputStream(u.openStream()); 

       byte[] buffer = new byte[contentLength]; 
       stream.readFully(buffer); 
       stream.close(); 

       DataOutputStream fos = new DataOutputStream(new FileOutputStream(new File(params[1]))); 
       fos.write(buffer); 
       fos.flush(); 
       fos.close(); 
      } catch(Exception e) { 
       Log.e("theple", "" + e); 
      } 
     return null; 
    } 
} 
0

有可以執行下載,在-儘管爲它創造一個方法,你應該使用「線程,異步類或服務」的許多方面,該錯誤「網絡主線程歸因於該過程需要時間「。

我顯示使用異步任務,並與服務

*

*class DownloadFile extends AsyncTask<String, Integer, String> { 
    @Override 
    protected String doInBackground(String... sUrl) { 
     try { 
      URL url = new URL(sUrl[0]); 
      URLConnection connection = url.openConnection(); 
      connection.connect(); 
      // this will be useful so that you can show a typical 0-100% progress bar 
      int fileLength = connection.getContentLength(); 
      // download the file 
      InputStream input = new BufferedInputStream(url.openStream()); 
      OutputStream output = new FileOutputStream("/sdcard/file_name.extension"); 
      byte data[] = new byte[1024]; 
      long total = 0; 
      int count; 


      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       publishProgress((int) (total * 100/fileLength)); 
       output.write(data, 0, count); 
      } 
      output.flush(); 
      output.close(); 
      input.close(); 
     } catch (Exception e) { 
     } 
     return null; 
    }** 


And With Service 

you can use : 



    class DownloadService extends IntentService { 
     public static final int UPDATE_PROGRESS = 8344; 
     public DownloadService() { 
      super("DownloadService"); 
     } 
     @Override 
     protected void onHandleIntent(Intent intent) { 
      String urlToDownload = intent.getStringExtra("url"); 
      ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); 
      try { 
       URL url = new URL(urlToDownload); 
       URLConnection connection = url.openConnection(); 
       connection.connect(); 
       // this will be useful so that you can show a typical 0-100% progress bar 
       int fileLength = connection.getContentLength(); 
       // download the file 
       InputStream input = new BufferedInputStream(url.openStream()); 
       OutputStream output = new FileOutputStream("/sdcard/BarcodeScanner-debug.apk"); 
       byte data[] = new byte[1024]; 
       long total = 0; 
       int count; 
       while ((count = input.read(data)) != -1) { 
        total += count; 
        // publishing the progress.... 
        Bundle resultData = new Bundle(); 
        resultData.putInt("progress" ,(int) (total * 100/fileLength)); 
        receiver.send(UPDATE_PROGRESS, resultData); 
        output.write(data, 0, count); 
       } 
       output.flush(); 
       output.close(); 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Bundle resultData = new Bundle(); 
      resultData.putInt("progress" ,100); 
      receiver.send(UPDATE_PROGRESS, resultData); 
     } 
    } 

希望例子,這將是對你有幫助。