加入两个权限
一个是联网,另一个是读写SD卡
1 | < uses-permission android:name = "android.permission.INTERNET" ></ uses-permission > |
2 | < uses-permission android:name = "android.permission.WRITE_EXTERNAL_STORAGE" ></ uses-permission > |
下载地址是本人的另外一台主机,现在当服务器了,路径可以测试
http://210.30.12.1:8080/mp3/DJ.mp3
001 | import java.io.File; |
002 | import java.io.FileNotFoundException; |
003 | import java.io.FileOutputStream; |
004 | import java.io.IOException; |
005 | import java.io.InputStream; |
006 | import java.io.OutputStream; |
007 | import java.io.RandomAccessFile; |
008 | import java.net.MalformedURLException; |
009 | import java.net.URL; |
010 | import java.net.URLConnection; |
011 |
012 | import android.app.Activity; |
013 | import android.os.Bundle; |
014 | import android.os.Environment; |
015 | import android.os.Handler; |
016 | import android.os.Message; |
017 | import android.util.Log; |
018 | import android.view.View; |
019 | import android.view.View.OnClickListener; |
020 | import android.widget.Button; |
021 | import android.widget.ProgressBar; |
022 | import android.widget.TextView; |
023 | import android.widget.Toast; |
024 |
025 | public class FileDownProcessBarActivity extends Activity { |
026 | /** Called when the activity is first created. */ |
027 | private static final String Path= "http://210.30.12.1:8080/mp3/DJ.mp3" ; |
028 | private ProgressBar progressBar; |
029 | private TextView textView; |
030 | private Button button; |
031 | private int FileLength; |
032 | private int DownedFileLength= 0 ; |
033 | private InputStream inputStream; |
034 | private URLConnection connection; |
035 | private OutputStream outputStream; |
036 | @Override |
037 | public void onCreate(Bundle savedInstanceState) { |
038 | super .onCreate(savedInstanceState); |
039 | setContentView(R.layout.main); |
040 | progressBar=(ProgressBar) findViewById(R.id.progressBar1); |
041 | textView=(TextView) findViewById(R.id.textView2); |
042 | button=(Button) findViewById(R.id.button1); |
043 | button.setOnClickListener( new ButtonListener()); |
044 | } |
045 | class ButtonListener implements OnClickListener{ |
046 |
047 | @Override |
048 | public void onClick(View v) { |
049 | DownedFileLength= 0 ; |
050 | // TODO Auto-generated method stub |
051 | Thread thread= new Thread(){ |
052 | public void run(){ |
053 | try { |
054 | DownFile(Path); |
055 | } catch (Exception e) { |
056 | // TODO: handle exception |
057 | } |
058 | } |
059 | }; |
060 | thread.start(); |
061 | } |
062 | } |
063 | private Handler handler= new Handler() |
064 | { |
065 | public void handleMessage(Message msg) |
066 | { |
067 | if (!Thread.currentThread().isInterrupted()) { |
068 | switch (msg.what) { |
069 | case 0 : |
070 | progressBar.setMax(FileLength); |
071 | Log.i( "文件长度----------->" , progressBar.getMax()+ "" ); |
072 | break ; |
073 | case 1 : |
074 | progressBar.setProgress(DownedFileLength); |
075 | int x=DownedFileLength* 100 /FileLength; |
076 | textView.setText(x+ "%" ); |
077 | break ; |
078 | case 2 : |
079 | Toast.makeText(getApplicationContext(), "下载完成" , Toast.LENGTH_LONG).show(); |
080 | break ; |
081 | |
082 | default : |
083 | break ; |
084 | } |
085 | } |
086 | } |
087 | |
088 | }; |
089 |
090 | private void DownFile(String urlString) |
091 | { |
092 | |
093 | /* |
094 | * 连接到服务器 |
095 | */ |
096 | |
097 | try { |
098 | URL url=new URL(urlString); |
099 | connection=url.openConnection(); |
100 | if (connection.getReadTimeout()==5) { |
101 | Log.i("---------->", "当前网络有问题"); |
102 | // return; |
103 | } |
104 | inputStream=connection.getInputStream(); |
105 | |
106 | } catch (MalformedURLException e1) { |
107 | // TODO Auto-generated catch block |
108 | e1.printStackTrace(); |
109 | } catch (IOException e) { |
110 | // TODO Auto-generated catch block |
111 | e.printStackTrace(); |
112 | } |
113 | |
114 | /* |
115 | * 文件的保存路径和和文件名其中Nobody.mp3是在手机SD卡上要保存的路径,如果不存在则新建 |
116 | */ |
117 | String savePAth=Environment.getExternalStorageDirectory()+"/DownFile"; |
118 | File file1=new File(savePAth); |
119 | if (!file1.exists()) { |
120 | file1.mkdir(); |
121 | } |
122 | String savePathString=Environment.getExternalStorageDirectory()+"/DownFile/"+"DJ.mp3"; |
123 | File file =new File(savePathString); |
124 | if (!file.exists()) { |
125 | try { |
126 | file.createNewFile(); |
127 | } catch (IOException e) { |
128 | // TODO Auto-generated catch block |
129 | e.printStackTrace(); |
130 | } |
131 | } |
132 | /* |
133 | * 向SD卡中写入文件,用Handle传递线程 |
134 | */ |
135 | Message message= new Message(); |
136 | try { |
137 | outputStream= new FileOutputStream(file); |
138 | byte [] buffer= new byte [ 1024 * 4 ]; |
139 | FileLength=connection.getContentLength(); |
140 | message.what= 0 ; |
141 | handler.sendMessage(message); |
142 | while (DownedFileLength<FileLength) { |
143 | outputStream.write(buffer); |
144 | DownedFileLength+=inputStream.read(buffer); |
145 | Log.i( "-------->" , DownedFileLength+ "" ); |
146 | Message message1= new Message(); |
147 | message1.what= 1 ; |
148 | handler.sendMessage(message1); |
149 | } |
150 | Message message2= new Message(); |
151 | message2.what= 2 ; |
152 | handler.sendMessage(message2); |
153 | } catch (FileNotFoundException e) { |
154 | // TODO Auto-generated catch block |
155 | e.printStackTrace(); |
156 | } catch (IOException e) { |
157 | // TODO Auto-generated catch block |
158 | e.printStackTrace(); |
159 | } |
160 | } |
161 | |
162 | } |