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", "--->
时间:2011-10-11 11:53 来源:LinuxIDC.com 作者:LinuxIDC.com 原文链接