从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);
	}

}
暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心