Android 下载文件 显示进度条
加入两个权限
一个是联网,另一个是读写SD卡
1 2 | < 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | 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(); } } } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法