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; @Override protected 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() { @Override public 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() { @Override public 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() { @Override public void onClick(View v) { handler.removeCallbacks(runnable); Log.i(TAG, "EndButton is pressed....."); endButton.setClickable(false); startButton.setClickable(true); } }); } Runnable runnable = new Runnable() { @Override public 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 block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (inputStream != null) inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.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" />
Android 下载文件至SD卡,并用progressBar显示下载进度
最新推荐文章于 2024-06-28 17:11:03 发布