歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 下載文件 進度條顯示

加入兩個權限

一個是聯網,另一個是讀寫SD卡

  1. <uses-permission Android:name="android.permission.INTERNET"></uses-permission>  
  2. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  

下載地址是本人的另外一台主機,現在當服務器了,路徑可以測試

http://210.30.12.1:8080/mp3/DJ.mp3

  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7. import java.io.RandomAccessFile;  
  8. import java.net.MalformedURLException;  
  9. import java.net.URL;  
  10. import java.net.URLConnection;  
  11.   
  12. import android.app.Activity;  
  13. import android.os.Bundle;  
  14. import android.os.Environment;  
  15. import android.os.Handler;  
  16. import android.os.Message;  
  17. import android.util.Log;  
  18. import android.view.View;  
  19. import android.view.View.OnClickListener;  
  20. import android.widget.Button;  
  21. import android.widget.ProgressBar;  
  22. import android.widget.TextView;  
  23. import android.widget.Toast;  
  24.   
  25. public class FileDownProcessBarActivity extends Activity {  
  26.     /** Called when the activity is first created. */  
  27.     private static final String Path="http://210.30.12.1:8080/mp3/DJ.mp3";  
  28.     private ProgressBar progressBar;  
  29.     private TextView textView;  
  30.     private Button button;  
  31.     private int FileLength;  
  32.     private int DownedFileLength=0;  
  33.     private InputStream inputStream;  
  34.     private URLConnection connection;  
  35.     private OutputStream outputStream;  
  36.     @Override  
  37.     public void onCreate(Bundle savedInstanceState) {  
  38.         super.onCreate(savedInstanceState);  
  39.         setContentView(R.layout.main);  
  40.         progressBar=(ProgressBar) findViewById(R.id.progressBar1);  
  41.         textView=(TextView) findViewById(R.id.textView2);  
  42.         button=(Button) findViewById(R.id.button1);  
  43.         button.setOnClickListener(new ButtonListener());  
  44.     }  
  45.     class ButtonListener implements OnClickListener{  
  46.   
  47.         @Override  
  48.         public void onClick(View v) {  
  49.             DownedFileLength=0;  
  50.             // TODO Auto-generated method stub   
  51.            Thread thread=new Thread(){  
  52.              public void run(){  
  53.                  try {  
  54.                     DownFile(Path);  
  55.                 } catch (Exception e) {  
  56.                     // TODO: handle exception   
  57.                 }  
  58.              }  
  59.            };  
  60.            thread.start();  
  61.         }     
  62.     }  
  63.     private Handler handler=new Handler()  
  64.     {  
  65.          public void handleMessage(Message msg)  
  66.         {  
  67.         if (!Thread.currentThread().isInterrupted()) {  
  68.             switch (msg.what) {  
  69.             case 0:  
  70.                 progressBar.setMax(FileLength);  
  71.                 Log.i("文件長度----------->", progressBar.getMax()+"");    
  72.                 break;  
  73.             case 1:  
  74.                 progressBar.setProgress(DownedFileLength);  
  75.                 int x=DownedFileLength*100/FileLength;  
  76.                 textView.setText(x+"%");  
  77.                 break;  
  78.             case 2:  
  79.                 Toast.makeText(getApplicationContext(), "下載完成", Toast.LENGTH_LONG).show();  
  80.                 break;  
  81.                   
  82.             default:  
  83.                 break;  
  84.             }  
  85.         }     
  86.         }  
  87.            
  88.     };  
  89.   
  90.     private void DownFile(String urlString)  
  91.     {  
  92.           
  93.         /* 
  94.          * 連接到服務器 
  95.          */  
  96.           
  97.         try {  
  98.              URL url=new URL(urlString);  
  99.              connection=url.openConnection();  
  100.              if (connection.getReadTimeout()==5) {  
  101.                 Log.i("---------->""當前網絡有問題");  
  102.                 // return;   
  103.                }  
  104.              inputStream=connection.getInputStream();  
  105.               
  106.         } catch (MalformedURLException e1) {  
  107.             // TODO Auto-generated catch block   
  108.             e1.printStackTrace();  
  109.         } catch (IOException e) {  
  110.             // TODO Auto-generated catch block   
  111.             e.printStackTrace();  
  112.         }  
  113.           
  114.         /* 
  115.          * 文件的保存路徑和和文件名其中Nobody.mp3是在手機SD卡上要保存的路徑,如果不存在則新建 
  116.          */  
  117.         String savePAth=Environment.getExternalStorageDirectory()+"/DownFile";  
  118.         File file1=new File(savePAth);  
  119.         if (!file1.exists()) {  
  120.             file1.mkdir();  
  121.         }  
  122.         String savePathString=Environment.getExternalStorageDirectory()+"/DownFile/"+"DJ.mp3";  
  123.         File file =new File(savePathString);  
  124.         if (!file.exists()) {  
  125.             try {  
  126.                 file.createNewFile();  
  127.             } catch (IOException e) {  
  128.                 // TODO Auto-generated catch block   
  129.                 e.printStackTrace();  
  130.             }     
  131.         }  
  132.         /* 
  133.          * 向SD卡中寫入文件,用Handle傳遞線程 
  134.          */  
  135.         Message message=new Message();  
  136.         try {  
  137.             outputStream=new FileOutputStream(file);  
  138.             byte [] buffer=new byte[1024*4];  
  139.             FileLength=connection.getContentLength();  
  140.             message.what=0;  
  141.             handler.sendMessage(message);  
  142.             while (DownedFileLength<FileLength) {  
  143.                 outputStream.write(buffer);  
  144.                 DownedFileLength+=inputStream.read(buffer);  
  145.                 Log.i("-------->", DownedFileLength+"");  
  146.                 Message message1=new Message();  
  147.                 message1.what=1;  
  148.                 handler.sendMessage(message1);  
  149.             }  
  150.             Message message2=new Message();  
  151.             message2.what=2;  
  152.             handler.sendMessage(message2);  
  153.         } catch (FileNotFoundException e) {  
  154.             // TODO Auto-generated catch block   
  155.             e.printStackTrace();  
  156.         } catch (IOException e) {  
  157.             // TODO Auto-generated catch block   
  158.             e.printStackTrace();  
  159.         }  
  160.     }  
  161.         
  162. }  
Copyright © Linux教程網 All Rights Reserved