

加入两个权限
一个是联网,另一个是读写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
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;
|
010 |
import java.net.URLConnection;
|
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;
|
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;
|
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());
|
045 |
class
ButtonListener implements
OnClickListener{
|
048 |
public
void onClick(View v) {
|
051 |
Thread thread= new
Thread(){
|
055 |
}
catch (Exception e) {
|
063 |
private
Handler handler= new
Handler()
|
065 |
public
void handleMessage(Message msg)
|
067 |
if
(!Thread.currentThread().isInterrupted()) {
|
070 |
progressBar.setMax(FileLength);
|
071 |
Log.i( "文件长度----------->" , progressBar.getMax()+ "" );
|
074 |
progressBar.setProgress(DownedFileLength);
|
075 |
int
x=DownedFileLength* 100 /FileLength;
|
076 |
textView.setText(x+ "%" );
|
079 |
Toast.makeText(getApplicationContext(),
"下载完成" , Toast.LENGTH_LONG).show();
|
090 |
private
void DownFile(String urlString)
|
135 |
Message message= new
Message();
|
137 |
outputStream= new
FileOutputStream(file);
|
138 |
byte
[] buffer= new
byte [ 1024 * 4 ];
|
139 |
FileLength=connection.getContentLength();
|
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();
|
148 |
handler.sendMessage(message1);
|
150 |
Message message2= new
Message();
|
152 |
handler.sendMessage(message2);
|
153 |
}
catch (FileNotFoundException e) {
|
156 |
}
catch (IOException e) {
|