Android開發:從Tomcat上下載MP3 帶百分比進度條
- package com.src.fpkj.android;
-
- import com.src.fpkj.android.down.DownFielToSdcard;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- /**
- * 實現一個帶進度條的下載dialog顯示百分比,很喜歡這效果,感覺很真切
- * @author 1314HWL
- * 2011/10/10/23:01
- */
- public class MainActivity extends Activity implements OnClickListener {
- Button btn_downMp3;
- String httpUrl = "http://10.0.2.2:8080/webdav/missyou.mp3";
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- btn_downMp3 = (Button) findViewById(R.id.btn_down);
- btn_downMp3.setOnClickListener(this);
- }
-
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.btn_down:
- DownFielToSdcard filedown = new DownFielToSdcard(MainActivity.this);
- try {
- //httpUrl:tomcat 下載地址 test/sdcard中得路徑
-
- filedown.LoadToSdcard(httpUrl, "test/", "missyou.mp3");
- } catch (Exception e1) {
- e1.printStackTrace();
- }
- break;
- }
-
- }
- }
-
-
- package com.src.fpkj.android.down;
-
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import com.src.fpkj.android.R;
- import android.app.AlertDialog;
- import android.app.Dialog;
- import android.content.Context;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
-
- public class DownFielToSdcard {
-
- private static String SDPath;
- ProgressBar pb;
- TextView tv_percent;
- int downLoadFileSize, tatalsize; //downLoadFileSize下載了多少, tatalsize總大小
- Dialog dialog;
- Context context;
-
- public DownFielToSdcard(Context context) {
- super();
- this.context = context;
- SDPath = Environment.getExternalStorageDirectory() + "/";// 得到的是/sdcard/
- }
-
- /**
- * 在sdcard中創建文件
- *
- * @param fileName
- * @return
- * @throws Exception
- */
- public File CreateFile(String fileName) throws Exception {
- File file = new File(SDPath + fileName);
- file.createNewFile();
- return file;
- }
-
- /**
- * 創建目錄
- *
- * @param fileName
- * @return
- * @throws Exception
- */
- public File CreateFileSdDir(String dirName) throws Exception {
- File sdDir = new File(SDPath + dirName);
- sdDir.mkdir();
- return sdDir;
- }
-
- /**
- * 判斷文件是否存在
- *
- * @param fileName
- * @return
- */
-
- public boolean FileExist(String fileName) {
- File file = new File(SDPath + fileName);
- return file.exists();
- }
-
- /**
- * 思路:要下載文件,先得創建目錄
- */
-
- public void LoadToSdcard(final String strUrl, final String path,
- final String fileName) throws Exception {
- if (FileExist("test/missyou.mp3")) {
- Toast.makeText(context, R.string.filehaved, Toast.LENGTH_LONG)
- .show();
- } else {
-
- View view = LayoutInflater.from(context).inflate(
- R.layout.download_dialog_xml, null);
- pb = (ProgressBar) view.findViewById(R.id.down_pb);
- tv_percent = (TextView) view.findViewById(R.id.pro_int);
- dialog = AlertDialogUtil(view, context,
- context.getString(R.string.waittingloading));
- new Thread(new Runnable() {
- public void run() {
- try {
- URL url = new URL(strUrl);
- HttpURLConnection conection = (HttpURLConnection) url
- .openConnection();
- tatalsize = conection.getContentLength();
- InputStream input = conection.getInputStream();
- File file = null;
- OutputStream outputstream = null;
- CreateFileSdDir(path);
- file = CreateFile(path + fileName);
- outputstream = new FileOutputStream(file);
- byte data[] = new byte[1024 * 4];
- sentMassage(0);
- while (true) {
- int temp = input.read(data);
- if (temp == -1) {
- break;
- }
- outputstream.write(data, 0, temp);
- downLoadFileSize += temp;
- sentMassage(1);
- }
- sentMassage(2);
- outputstream.flush();
- outputstream.close();
- input.close();
- } catch (Exception e) {
- Toast.makeText(context, R.string.app_falls,
- Toast.LENGTH_LONG).show();
- e.printStackTrace();
- }
- }
-
- }).start();
- }
- }
-
- /**
- * 返回一個dialog
- *
- * @param view
- * @param context
- * @param string
- * @return
- */
- private Dialog AlertDialogUtil(View view, Context context, String string) {
- AlertDialog.Builder builder = new AlertDialog.Builder(context);
- builder.setTitle(string);
- builder.setIcon(R.drawable.icon);
- builder.setView(view);
- builder.create();
- return builder.show();
- }
-
- /**
- * handler 處理動作
- */
- Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case 0:
- pb.setMax(tatalsize);
- break;
- case 1:
- pb.setProgress(downLoadFileSize);
- int result = downLoadFileSize * 100 / tatalsize;
- tv_percent.setText(context.getString(R.string.fileload)
- + result + "%");
- break;
- case 2:
- dialog.dismiss();
- Toast.makeText(context, R.string.loagsucces, Toast.LENGTH_LONG)
- .show();
- Log.v("test", "--->>> " + "end");
- break;
- }
-
- }
- };
-
- /**
- *
- * @param flag
- * 消息類型
- */
-
- public void sentMassage(int flag) {
- Message msg = new Message();
- msg.what = flag;
- handler.sendMessage(msg);
- }
-
- }
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, MainActivity!</string>
- <string name="app_name">LoadDownFile</string>
- <string name="loagsucces">下載成功</string>
- <string name="app_falls">下載失敗</string>
- <string name="waittingloading">正在下載請稍後……</string>
- <string name="filehaved">文件已存在</string>
- <string name="fileload">文件下載</string>
- </resources>