歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開發:從Tomcat上下載MP3 帶百分比進度條

Android開發:從Tomcat上下載MP3 帶百分比進度條

日期:2017/3/1 11:13:09   编辑:Linux編程

Android開發:從Tomcat上下載MP3 帶百分比進度條

  1. package com.src.fpkj.android;
  2. import com.src.fpkj.android.down.DownFielToSdcard;
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. /**
  9. * 實現一個帶進度條的下載dialog顯示百分比,很喜歡這效果,感覺很真切
  10. * @author 1314HWL
  11. * 2011/10/10/23:01
  12. */
  13. public class MainActivity extends Activity implements OnClickListener {
  14. Button btn_downMp3;
  15. String httpUrl = "http://10.0.2.2:8080/webdav/missyou.mp3";
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. btn_downMp3 = (Button) findViewById(R.id.btn_down);
  20. btn_downMp3.setOnClickListener(this);
  21. }
  22. public void onClick(View v) {
  23. switch (v.getId()) {
  24. case R.id.btn_down:
  25. DownFielToSdcard filedown = new DownFielToSdcard(MainActivity.this);
  26. try {
  27. //httpUrl:tomcat 下載地址 test/sdcard中得路徑
  28. filedown.LoadToSdcard(httpUrl, "test/", "missyou.mp3");
  29. } catch (Exception e1) {
  30. e1.printStackTrace();
  31. }
  32. break;
  33. }
  34. }
  35. }
  36. package com.src.fpkj.android.down;
  37. import java.io.File;
  38. import java.io.FileOutputStream;
  39. import java.io.InputStream;
  40. import java.io.OutputStream;
  41. import java.net.HttpURLConnection;
  42. import java.net.URL;
  43. import com.src.fpkj.android.R;
  44. import android.app.AlertDialog;
  45. import android.app.Dialog;
  46. import android.content.Context;
  47. import android.os.Environment;
  48. import android.os.Handler;
  49. import android.os.Message;
  50. import android.util.Log;
  51. import android.view.LayoutInflater;
  52. import android.view.View;
  53. import android.widget.ProgressBar;
  54. import android.widget.TextView;
  55. import android.widget.Toast;
  56. public class DownFielToSdcard {
  57. private static String SDPath;
  58. ProgressBar pb;
  59. TextView tv_percent;
  60. int downLoadFileSize, tatalsize; //downLoadFileSize下載了多少, tatalsize總大小
  61. Dialog dialog;
  62. Context context;
  63. public DownFielToSdcard(Context context) {
  64. super();
  65. this.context = context;
  66. SDPath = Environment.getExternalStorageDirectory() + "/";// 得到的是/sdcard/
  67. }
  68. /**
  69. * 在sdcard中創建文件
  70. *
  71. * @param fileName
  72. * @return
  73. * @throws Exception
  74. */
  75. public File CreateFile(String fileName) throws Exception {
  76. File file = new File(SDPath + fileName);
  77. file.createNewFile();
  78. return file;
  79. }
  80. /**
  81. * 創建目錄
  82. *
  83. * @param fileName
  84. * @return
  85. * @throws Exception
  86. */
  87. public File CreateFileSdDir(String dirName) throws Exception {
  88. File sdDir = new File(SDPath + dirName);
  89. sdDir.mkdir();
  90. return sdDir;
  91. }
  92. /**
  93. * 判斷文件是否存在
  94. *
  95. * @param fileName
  96. * @return
  97. */
  98. public boolean FileExist(String fileName) {
  99. File file = new File(SDPath + fileName);
  100. return file.exists();
  101. }
  102. /**
  103. * 思路:要下載文件,先得創建目錄
  104. */
  105. public void LoadToSdcard(final String strUrl, final String path,
  106. final String fileName) throws Exception {
  107. if (FileExist("test/missyou.mp3")) {
  108. Toast.makeText(context, R.string.filehaved, Toast.LENGTH_LONG)
  109. .show();
  110. } else {
  111. View view = LayoutInflater.from(context).inflate(
  112. R.layout.download_dialog_xml, null);
  113. pb = (ProgressBar) view.findViewById(R.id.down_pb);
  114. tv_percent = (TextView) view.findViewById(R.id.pro_int);
  115. dialog = AlertDialogUtil(view, context,
  116. context.getString(R.string.waittingloading));
  117. new Thread(new Runnable() {
  118. public void run() {
  119. try {
  120. URL url = new URL(strUrl);
  121. HttpURLConnection conection = (HttpURLConnection) url
  122. .openConnection();
  123. tatalsize = conection.getContentLength();
  124. InputStream input = conection.getInputStream();
  125. File file = null;
  126. OutputStream outputstream = null;
  127. CreateFileSdDir(path);
  128. file = CreateFile(path + fileName);
  129. outputstream = new FileOutputStream(file);
  130. byte data[] = new byte[1024 * 4];
  131. sentMassage(0);
  132. while (true) {
  133. int temp = input.read(data);
  134. if (temp == -1) {
  135. break;
  136. }
  137. outputstream.write(data, 0, temp);
  138. downLoadFileSize += temp;
  139. sentMassage(1);
  140. }
  141. sentMassage(2);
  142. outputstream.flush();
  143. outputstream.close();
  144. input.close();
  145. } catch (Exception e) {
  146. Toast.makeText(context, R.string.app_falls,
  147. Toast.LENGTH_LONG).show();
  148. e.printStackTrace();
  149. }
  150. }
  151. }).start();
  152. }
  153. }
  154. /**
  155. * 返回一個dialog
  156. *
  157. * @param view
  158. * @param context
  159. * @param string
  160. * @return
  161. */
  162. private Dialog AlertDialogUtil(View view, Context context, String string) {
  163. AlertDialog.Builder builder = new AlertDialog.Builder(context);
  164. builder.setTitle(string);
  165. builder.setIcon(R.drawable.icon);
  166. builder.setView(view);
  167. builder.create();
  168. return builder.show();
  169. }
  170. /**
  171. * handler 處理動作
  172. */
  173. Handler handler = new Handler() {
  174. public void handleMessage(Message msg) {
  175. super.handleMessage(msg);
  176. switch (msg.what) {
  177. case 0:
  178. pb.setMax(tatalsize);
  179. break;
  180. case 1:
  181. pb.setProgress(downLoadFileSize);
  182. int result = downLoadFileSize * 100 / tatalsize;
  183. tv_percent.setText(context.getString(R.string.fileload)
  184. + result + "%");
  185. break;
  186. case 2:
  187. dialog.dismiss();
  188. Toast.makeText(context, R.string.loagsucces, Toast.LENGTH_LONG)
  189. .show();
  190. Log.v("test", "--->>> " + "end");
  191. break;
  192. }
  193. }
  194. };
  195. /**
  196. *
  197. * @param flag
  198. * 消息類型
  199. */
  200. public void sentMassage(int flag) {
  201. Message msg = new Message();
  202. msg.what = flag;
  203. handler.sendMessage(msg);
  204. }
  205. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="hello">Hello World, MainActivity!</string>
  4. <string name="app_name">LoadDownFile</string>
  5. <string name="loagsucces">下載成功</string>
  6. <string name="app_falls">下載失敗</string>
  7. <string name="waittingloading">正在下載請稍後……</string>
  8. <string name="filehaved">文件已存在</string>
  9. <string name="fileload">文件下載</string>
  10. </resources>
Copyright © Linux教程網 All Rights Reserved