001.
package
com.myapp;
002.
003.
import
java.io.BufferedInputStream;
004.
import
java.io.FileOutputStream;
005.
import
java.io.InputStream;
006.
import
java.io.OutputStream;
007.
import
java.net.URL;
008.
import
java.net.URLConnection;
009.
010.
import
android.os.AsyncTask;
011.
import
android.os.Bundle;
012.
import
android.app.Activity;
013.
import
android.app.Dialog;
014.
import
android.app.ProgressDialog;
015.
import
android.util.Log;
016.
import
android.view.View;
017.
import
android.view.View.OnClickListener;
018.
import
android.widget.Button;
019.
import
android.widget.EditText;
020.
021.
public
class
MainActivity
extends
Activity {
022.
023.
public
static
final
int
DIALOG_DOWNLOAD_PROGRESS =
0
;
024.
private
Button startBtn;
025.
private
ProgressDialog mProgressDialog;
026.
private
String URLDownload;
027.
private
EditText txtURL;
028.
029.
030.
@Override
031.
public
void
onCreate(Bundle savedInstanceState) {
032.
super
.onCreate(savedInstanceState);
033.
setContentView(R.layout.activity_main);
034.
035.
036.
startBtn = (Button)findViewById(R.id.button1);
037.
startBtn.setOnClickListener(
new
OnClickListener(){
038.
public
void
onClick(View v) {
039.
startDownload();
040.
}
041.
});
042.
}
043.
044.
private
void
startDownload() {
045.
046.
txtURL = (EditText)findViewById(R.id.editText1);
047.
URLDownload = txtURL.getText().toString();
048.
new
DownloadFileAsync().execute(URLDownload);
049.
}
050.
@Override
051.
protected
Dialog onCreateDialog(
int
id) {
052.
switch
(id) {
053.
case
DIALOG_DOWNLOAD_PROGRESS:
054.
mProgressDialog =
new
ProgressDialog(
this
);
055.
mProgressDialog.setMessage(
"Downloading file.."
);
056.
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
057.
mProgressDialog.setCancelable(
false
);
058.
mProgressDialog.show();
059.
return
mProgressDialog;
060.
default
:
061.
return
null
;
062.
}
063.
}
064.
065.
class
DownloadFileAsync
extends
AsyncTask<String, String, String> {
066.
067.
@Override
068.
protected
void
onPreExecute() {
069.
super
.onPreExecute();
070.
showDialog(DIALOG_DOWNLOAD_PROGRESS);
071.
}
072.
073.
@Override
074.
protected
String doInBackground(String... aurl) {
075.
int
count;
076.
077.
try
{
078.
079.
URL url =
new
URL(aurl[
0
]);
080.
URLConnection conexion = url.openConnection();
081.
conexion.connect();
082.
083.
int
lenghtOfFile = conexion.getContentLength();
084.
Log.d(
"ANDRO_ASYNC"
,
"Lenght of file: "
+ lenghtOfFile);
085.
086.
InputStream input =
new
BufferedInputStream(url.openStream());
087.
088.
089.
String fileName = URLDownload.substring( URLDownload.lastIndexOf(
'/'
)+
1
, URLDownload.length() );
090.
091.
OutputStream output =
new
FileOutputStream(
"/sdcard/MyData/"
+fileName);
092.
093.
byte
data[] =
new
byte
[
1024
];
094.
095.
long
total =
0
;
096.
097.
while
((count = input.read(data)) != -
1
) {
098.
total += count;
099.
publishProgress(
""
+(
int
)((total*
100
)/lenghtOfFile));
100.
output.write(data,
0
, count);
101.
}
102.
103.
output.flush();
104.
output.close();
105.
input.close();
106.
}
catch
(Exception e) {}
107.
return
null
;
108.
109.
}
110.
protected
void
onProgressUpdate(String... progress) {
111.
Log.d(
"ANDRO_ASYNC"
,progress[
0
]);
112.
mProgressDialog.setProgress(Integer.parseInt(progress[
0
]));
113.
}
114.
115.
@Override
116.
protected
void
onPostExecute(String unused) {
117.
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
118.
}
119.
}
120.
}