URL을 이용해서 HTTP 기반으로 이미지를 다운로드하는 코드입니다.
일단, 인터넷 경로를 통해 다운을 받기 위해서는 AndroidManifest.xml파일에
permission을 추가해줘야 합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="Image.Downloader"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ImageDownloader"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
그리고 다음과 같은 코드를 이용해서 URL을 통해 이미지를 다운 받을 수 있습니다.
01.
private
Bitmap GetImageFromURL(String strImageURL)
02.
{
03.
Bitmap imgBitmap =
null
;
04.
05.
try
06.
{
07.
URL url =
new
URL(strImageURL);
08.
URLConnection conn = url.openConnection();
09.
conn.connect();
10.
11.
int
nSize = conn.getContentLength();
12.
BufferedInputStream bis =
new
BufferedInputStream(conn.getInputStream(), nSize);
13.
imgBitmap = BitmapFactory.decodeStream(bis);
14.
15.
bis.close();
16.
}
17.
catch
(Exception e)
18.
{
19.
e.printStackTrace();
20.
}
21.
22.
return
imgBitmap;
23.
}
전체 액티비티 소스는 다음과 같습니다.
01.
package
Image.Downloader;
02.
03.
import
java.io.BufferedInputStream;
04.
import
java.io.InputStream;
05.
import
java.net.URL;
06.
import
java.net.URLConnection;
07.
08.
import
org.apache.http.HttpEntity;
09.
import
org.apache.http.HttpResponse;
10.
import
org.apache.http.HttpStatus;
11.
import
org.apache.http.client.methods.HttpGet;
12.
13.
import
android.app.Activity;
14.
import
android.graphics.Bitmap;
15.
import
android.graphics.BitmapFactory;
16.
import
android.net.http.AndroidHttpClient;
17.
import
android.os.Bundle;
18.
import
android.view.View;
19.
import
android.widget.Button;
20.
import
android.widget.ImageView;
21.
22.
public
class
ImageDownloader
extends
Activity {
23.
/** Called when the activity is first created. */
24.
@Override
25.
public
void
onCreate(Bundle savedInstanceState) {
26.
super
.onCreate(savedInstanceState);
27.
setContentView(R.layout.main);
28.
29.
Button btnDownload = (Button)findViewById(R.id.btnButton);
30.
btnDownload.setOnClickListener(myButtonClick);
31.
}
32.
33.
Button.OnClickListener myButtonClick =
new
Button.OnClickListener()
34.
{
35.
public
void
onClick(View v)
36.
{
37.
Bitmap imgBitmap = GetImageFromURL(
"http://cfile25.uf.tistory.com/image/112CA2274C2220D2B47CB1"
);
38.
if
(imgBitmap !=
null
)
39.
{
40.
ImageView imgView = (ImageView)findViewById(R.id.ImageView);
41.
imgView.setImageBitmap(imgBitmap);
42.
}
43.
}
44.
};
45.
46.
private
Bitmap GetImageFromURL(String strImageURL)
47.
{
48.
Bitmap imgBitmap =
null
;
49.
50.
try
51.
{
52.
URL url =
new
URL(strImageURL);
53.
URLConnection conn = url.openConnection();
54.
conn.connect();
55.
56.
int
nSize = conn.getContentLength();
57.
BufferedInputStream bis =
new
BufferedInputStream(conn.getInputStream(), nSize);
58.
imgBitmap = BitmapFactory.decodeStream(bis);
59.
60.
bis.close();
61.
}
62.
catch
(Exception e)
63.
{
64.
e.printStackTrace();
65.
}
66.
67.
return
imgBitmap;
68.
}
69.
}