Android 下载文件至SD卡,并用progressBar显示下载进度

来源:互联网 发布:vb和易语言 编辑:程序博客网 时间:2024/05/17 06:30
Android 下载文件至SD卡,并用progressBar显示下载进度

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ProgressBar;import android.widget.Toast;public class ProgressBarActivity extends Activity {private final static String TAG = "MainActivity";private final static String downloadAddress = "http://e.hiphotos.baidu.com/image/w%3D2048/sign=96d9aaaf552c11dfded1b823571f63d0/eaf81a4c510fd9f9232a3825272dd42a2834a4b9.jpg";private final static String musicName = "美女.jpg";Button startButton = null;Button endButton = null;ProgressBar progressBar = null;Handler handler = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.progressbar);startButton = (Button) this.findViewById(R.id.start);endButton = (Button) this.findViewById(R.id.end);progressBar = (ProgressBar) this.findViewById(R.id.progressBar);progressBar.setMax(100);startButton.setClickable(true);endButton.setClickable(false);handler = new Handler() {@Overridepublic void handleMessage(Message msg) {int progress = msg.arg1;if (progress >= 99) {Toast.makeText(ProgressBarActivity.this,"download is finished", Toast.LENGTH_SHORT).show();}progressBar.setProgress(progress);super.handleMessage(msg);}};startButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// startOnClick();new Thread(runnable).start();Log.i(TAG, "StartButton is pressed.....");startButton.setClickable(false);endButton.setClickable(true);}});endButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {handler.removeCallbacks(runnable);Log.i(TAG, "EndButton is pressed.....");endButton.setClickable(false);startButton.setClickable(true);}});}Runnable runnable = new Runnable() {@Overridepublic void run() {URL url = null;InputStream inputStream = null;FileOutputStream fileOutputStream = null;try {// 设置下载路径String path = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + musicName;// 检查SD卡状态if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {fileOutputStream = new FileOutputStream(path);url = new URL(downloadAddress);HttpURLConnection connection = (HttpURLConnection) url.openConnection();inputStream = connection.getInputStream();long total_length = connection.getContentLength();byte[] buf = new byte[1024];int len = 0;int now_length = 0;while ((len = inputStream.read(buf)) != -1) {fileOutputStream.write(buf, 0, len);now_length += len;Message msg = handler.obtainMessage();//计算下载进度msg.arg1 = ((int) ((now_length / (float) total_length) * 100));msg.sendToTarget();}} else {Toast.makeText(ProgressBarActivity.this,"Please check SD card", Toast.LENGTH_SHORT).show();}} catch (MalformedURLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {try {if (inputStream != null)inputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}};}

别忘了要加上权限
<!-- sdCard增加删除与读写权限 --><uses-permission android:name="android.permission.INTERNET" />


    <!-- sdCard增加删除与读写权限 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
                                             
0 0
原创粉丝点击