๋ฐ์ํ
์ด๋ฏธ์ง url๋ก ์ด๋ฏธ์ง๋ฅผ ๋ค์ด๋ฐ์ ํด๋์ ์ ์ฅํ๊ณ ์ ์ฅํ ์ด๋ฏธ์ง๋ฅผ ๊ฐค๋ฌ๋ฆฌ๋ฅผ ํตํด ์ฌ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
1. ํผ๋ฏธ์ ์ค์ (AndroidManifest.xml)
<!-- ์ธํฐ๋ท ์ ์ -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- ํ์ผ ์์ฑ,์ญ์ ํ๊ธฐ ์ํ -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. ์๋ฐ ์ฝ๋ ์์ฑ (MainActivity.java)
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String imgUrl = "http://img.naver.net/static/www/u/2013/0731/nmms_224940510.gif";
new ImageDownload().execute(imgUrl);
}
/**
* url ์ด๋ฏธ์ง ๋ค์ด๋ก๋
*
*/
private class ImageDownload extends AsyncTask<String, Void, Void> {
/**
* ํ์ผ๋ช
*/
private String fileName;
/**
* ์ ์ฅํ ํด๋
*/
private final String SAVE_FOLDER = "/save_folder";
@Override
protected Void doInBackground(String... params) {
//๋ค์ด๋ก๋ ๊ฒฝ๋ก๋ฅผ ์ง์
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 = params[0];
//๋ค์ด๋ก๋ ํด๋์ ๋์ผํ ํ์ผ๋ช
์ด ์กด์ฌํ๋์ง ํ์ธ
if (new File(savePath + "/" + fileName).exists() == false) {
} else {
}
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); //file ์์ฑ
}
is.close();
fos.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
//์ ์ฅํ ์ด๋ฏธ์ง ์ด๊ธฐ
Intent i = new Intent(Intent.ACTION_VIEW);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
String targetDir = Environment.getExternalStorageDirectory().toString() + SAVE_FOLDER;
File file = new File(targetDir + "/" + fileName + ".jpg");
//type ์ง์ (์ด๋ฏธ์ง)
i.setDataAndType(Uri.fromFile(file), "image/*");
getApplicationContext().startActivity(i);
//์ด๋ฏธ์ง ์ค์บํด์ ๊ฐค๋ฌ๋ฆฌ ์
๋ฐ์ดํธ
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
}
}
}
์ฑ์ ์คํํ๋ฉด ํด๋์ ์๋์ผ๋ก ์ ์ฅ๋๊ณ ์ ์ฅํ ์ด๋ฏธ์ง๋ฅผ ๊ฐค๋ฌ๋ฆฌ์์ ๋ฐ๋ก ์ด์ด์ค๋๋ค.
[๊ฒฐ๊ณผํ๋ฉด]