Android 下載文件 顯示進度條
加入兩個權限
一個是聯網,另一個是讀寫SD卡
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
下載地址是本人的另外一臺主機,現在當服務器了,路徑可以測試
http://210.30.12.1:8080/mp3/DJ.mp3
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; 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.TextView; import android.widget.Toast; public class FileDownProcessBarActivity extends Activity { /** Called when the activity is first created. */ private static final String Path="http://210.30.12.1:8080/mp3/DJ.mp3"; private ProgressBar progressBar; private TextView textView; private Button button; private int FileLength; private int DownedFileLength=0; private InputStream inputStream; private URLConnection connection; private OutputStream outputStream; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar=(ProgressBar) findViewById(R.id.progressBar1); textView=(TextView) findViewById(R.id.textView2); button=(Button) findViewById(R.id.button1); button.setOnClickListener(new ButtonListener()); } class ButtonListener implements OnClickListener{ @Override public void onClick(View v) { DownedFileLength=0; // TODO Auto-generated method stub Thread thread=new Thread(){ public void run(){ try { DownFile(Path); } catch (Exception e) { // TODO: handle exception } } }; thread.start(); } } private Handler handler=new Handler() { public void handleMessage(Message msg) { if (!Thread.currentThread().isInterrupted()) { switch (msg.what) { case 0: progressBar.setMax(FileLength); Log.i("文件長度----------->", progressBar.getMax()+""); break; case 1: progressBar.setProgress(DownedFileLength); int x=DownedFileLength*100/FileLength; textView.setText(x+"%"); break; case 2: Toast.makeText(getApplicationContext(), "下載完成", Toast.LENGTH_LONG).show(); break; default: break; } } } }; private void DownFile(String urlString) { /* * 連接到服務器 */ try { URL url=new URL(urlString); connection=url.openConnection(); if (connection.getReadTimeout()==5) { Log.i("---------->", "當前網絡有問題"); // return; } inputStream=connection.getInputStream(); } catch (MalformedURLException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } /* * 文件的保存路徑和和文件名其中Nobody.mp3是在手機SD卡上要保存的路徑,如果不存在則新建 */ String savePAth=Environment.getExternalStorageDirectory()+"/DownFile"; File file1=new File(savePAth); if (!file1.exists()) { file1.mkdir(); } String savePathString=Environment.getExternalStorageDirectory()+"/DownFile/"+"DJ.mp3"; File file =new File(savePathString); if (!file.exists()) { try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * 向SD卡中寫入文件,用Handle傳遞線程 */ Message message=new Message(); try { outputStream=new FileOutputStream(file); byte [] buffer=new byte[1024*4]; FileLength=connection.getContentLength(); message.what=0; handler.sendMessage(message); while (DownedFileLength<FileLength) { outputStream.write(buffer); DownedFileLength+=inputStream.read(buffer); Log.i("-------->", DownedFileLength+""); Message message1=new Message(); message1.what=1; handler.sendMessage(message1); } Message message2=new Message(); message2.what=2; handler.sendMessage(message2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!