2011-02-18 38 views
3

我是Android開發新手。在Android中實現文件下載的進度條

我寫了下面的代碼從 互聯網下載視頻文件。它工作正常。現在我想在 下載過程中附加一個進度條。我嘗試了AsyncTask的子類並在doInBackground()方法內寫入下載代碼。但是,不知何故,我無法弄清楚。

有人可以幫我修改這段代碼來完成這個嗎?


package sample.android.download; 

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.URL; 
import java.net.URLConnection; 

import org.apache.http.util.ByteArrayBuffer; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 
import android.util.Log; 
import android.widget.TextView; 

public class DownloadDemo extends Activity { 

    private TextView tv; 

    private String videoURL = "http://mysite-name.com/videos/videofile_name.mp4"; 
    private String fileName = "my_video.mp4"; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tv = (TextView) findViewById(R.id.TextView01); 
     if(checkExternalMedia()==true) { 
       DownloadFromUrl(videoURL,fileName); 
       tv.append("\n\nDownload Complete!"); 
     } 
     else { 
       tv.append("\n\nExternal Media is NOT readable/writable"); 
     } 
    } 

    /** Method to check whether external media available and writable. */ 

    private boolean checkExternalMedia(){ 
     boolean mExternalStorageAvailable = false; 
     boolean mExternalStorageWriteable = false; 
     boolean stat; 
     String state = Environment.getExternalStorageState(); 

     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      // Can read and write the media 
      mExternalStorageAvailable = mExternalStorageWriteable = true; 
      stat = true; 
     } 
     else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
      // Can only read the media 
      mExternalStorageAvailable = true; 
      mExternalStorageWriteable = false; 
      stat = false; 
     } else { 
      // Can't read or write 
      mExternalStorageAvailable = mExternalStorageWriteable = false; 
      stat = false; 
     } 
     tv.append("\n\nExternal Media: readable="+mExternalStorageAvailable+ "writable="+mExternalStorageWriteable); 

     return stat; 
    } 

    /** Method to download an external file from the network to the SD card. */ 

    public void DownloadFromUrl(String videoURL, String fileName) { 

     try { 
        File root = android.os.Environment.getExternalStorageDirectory(); 
        tv.append("\nExternal file system root: "+root); 

        File dir = new File (root.getAbsolutePath() + "/video"); 
        //dir.mkdirs(); 

        URL url = new URL(videoURL); //you can write here any link 
        File file = new File(dir, fileName); 

        long startTime = System.currentTimeMillis(); 
        Log.d("ImageManager", "download begining"); 
        Log.d("ImageManager", "download url:" + url); 
        Log.d("ImageManager", "downloaded file name:" + fileName); 

        /* Open a connection to that URL. */ 
        URLConnection ucon = url.openConnection(); 

        /* 
        * Define InputStreams to read from the URLConnection. 
        */ 
        InputStream is = ucon.getInputStream(); 
        BufferedInputStream bis = new BufferedInputStream(is); 

        /* 
        * Read bytes to the Buffer until there is nothing more to read(-1). 
        */ 
        ByteArrayBuffer baf = new ByteArrayBuffer(5000); 
        int current = 0; 
        while ((current = bis.read()) != -1) { 
         baf.append((byte) current); 
        } 


        /* Convert the Bytes read to a String. */ 
        FileOutputStream fos = new FileOutputStream(file); 
        fos.write(baf.toByteArray()); 
        fos.flush(); 
        fos.close(); 
        Log.d("ImageManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 

     } catch (IOException e) { 
         Log.d("ImageManager", "Error: " + e); 
     } 

     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
    } 
} 
+0

AsyncTask的哪個部分有問題? [documentation](http://developer.android.com/reference/android/os/AsyncTask.html)和[示例](http://developer.android.com/resources/articles/painless-threading.html)相當清楚。也許你可以解釋你遇到的問題? – 2011-02-18 13:55:35

回答