728x90
반응형
이미지 다운로드
이미지 다운로드 코드
public class ImageDownload extends AsyncTask<String, Void, Void> {
private String fileName = "Name";
private final String SAVE_FOLDER = "/Download/";
@Override
protected Void doInBackground(String... strings) {
String savePath = Environment.getExternalStorageDirectory().toString()+SAVE_FOLDER;
File dir = new File(savePath);
if (!dir.exists()) {
dir.mkdirs();
}
Date day=new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.KOREA);
fileName += String.valueOf(sdf.format(day));
String fileUrl = strings[0];
String localPath = savePath + fileName + ".jpg";
try {
URL imgUrl = new URL(fileUrl);
HttpURLConnection conn = (HttpURLConnection) imgUrl.openConnection();
int len = conn.getContentLength();
byte[] tmpByte = new byte[len];
InputStream is = conn.getInputStream();
File file = new File(localPath);
FileOutputStream fos=new FileOutputStream(file);
int read;
for (; ; ) {
read = is.read(tmpByte);
if (read <= 0) {
break;
}
fos.write(tmpByte, 0, read);
}
App.getContext().sendBroadcast(new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)) );
is.close();
fos.close();
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
728x90
반응형
'Android' 카테고리의 다른 글
Android 리스트뷰 키보드 올라올때 화면 (0) | 2019.10.15 |
---|---|
Android 그라데이션 (2) | 2019.10.02 |
Android 웹 연결 안될때 (0) | 2019.09.26 |
Android 현재 시간 구하기 (0) | 2019.09.25 |
Android 클릭 효과 넣기 (0) | 2019.09.04 |