public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File_Name =
"123.png"
;
File_extend =
"png"
;
Save_folder =
"/Download"
;
String ext = Environment.getExternalStorageState();
if
(ext.equals(Environment.MEDIA_MOUNTED)) {
Save_Path = Environment.getExternalStorageDirectory().getAbsolutePath() + Save_folder;
}
btn_pdfDownload = (Button)findViewById(R.id.btn_pdfDownload);
btn_pdfDownload.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
File dir =
new
File(Save_Path);
if
(!dir.exists()) {
dir.mkdir();
}
if
(!
new
File(Save_Path +
"/"
+ File_Name).exists()) {
progress = ProgressDialog.show(MainActivity.
this
,
""
,
"파일 다운로드중.."
);
dThread =
new
DownloadThread(fileURL, Save_Path +
"/"
+ File_Name);
dThread.start();
}
else
{
}
}
});
}
class
DownloadThread
extends
Thread {
String ServerUrl;
String LocalPath;
DownloadThread(String serverPath, String localPath) {
ServerUrl = serverPath;
LocalPath = localPath;
}
@Override
public
void
run() {
URL imgurl;
int
Read;
try
{
imgurl =
new
URL(ServerUrl);
HttpURLConnection conn = (HttpURLConnection) imgurl.openConnection();
conn.connect();
int
len = conn.getContentLength();
byte
[] tmpByte =
new
byte
[len];
InputStream is = conn.getInputStream();
File file =
new
File(LocalPath);
FileOutputStream fos =
new
FileOutputStream(file);
for
(;;) {
Read = is.read(tmpByte);
if
(Read <=
0
) {
break
;
}
fos.write(tmpByte,
0
, Read);
}
is.close();
fos.close();
conn.disconnect();
}
catch
(MalformedURLException e) {
Log.e(
"ERROR1"
, e.getMessage());
}
catch
(IOException e) {
Log.e(
"ERROR2"
, e.getMessage());
e.printStackTrace();
}
}
}