ȸ¿ø·Î±×ÀÎ
[Source] À̹ÌÁöÆÄÀÏ ´Ù¿î·Îµå
8³â Àü
// ImageDownloader.java
package co.kr.codein;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImageDownloader extends Activity {
Button imageBtn;
ImageView image;
ProgressDialog pd;
Handler mHandler;
Bitmap bmp;
String Url;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_dowload);
mHandler = new Handler();
Url = "http://tvzonedoc15.media.daum.net/pcp_download.php?fhandle=N2dZWkRAdHZ6b25lZG9jMTUubWVkaWEuZGF1bS5uZXQ6L0EwMDAwMDQvMjgvMjgwMi5qcGc=&filename=1.jpg";
imageBtn = (Button) findViewById(R.id.btn);
imageBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pd = ProgressDialog.show(ImageDownloader.this,
"Downloading Image", "Please Wait..", false);
Thread one = new Thread() {
public void run() {
getFile(Url);
mHandler.post(mUpdateResults);
}
};
one.start();
}
});
image = (ImageView) findViewById(R.id.my_view);
}
void getFile(String fileurl) {
URL myUrl = null;
try {
myUrl = new URL(fileurl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
} catch (IOException e) {
e.printStackTrace();
}
}
final Runnable mUpdateResults = new Runnable() {
public void run() {
pd.dismiss();
image.setImageBitmap(bmp);
// imageBtn.setVisibility(View.INVISIBLE);
}
};
}
// image_dowload.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/my_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
</ImageView>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:text="Click"
android:textStyle="bold"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
</Button>
</RelativeLayout>
̵̧ : 265
̵̧
¸ñ·Ï