歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 下載文件 進度條顯示

Android 下載文件 進度條顯示

日期:2017/3/1 11:16:34   编辑:Linux編程

加入兩個權限

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