28 5월

웹에 있는 이미지를 읽어서 ImageView에 출력

package yell.exam01;

import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import yell.exam01.R;

public class test_network extends Activity {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_network);
	}

	public void mOnClick(View v) {

		EditText et = (EditText) findViewById(R.id.textURL);
		ImageView iv = (ImageView) findViewById(R.id.imageview1);

		if (et.getText().toString() != null) {
			try {
				HttpGet httpget = new HttpGet(et.getText().toString());
				DefaultHttpClient client = new DefaultHttpClient();
				HttpResponse response = client.execute(httpget);
				InputStream is = new BufferedHttpEntity(response.getEntity()).getContent();

				Bitmap bit = BitmapFactory.decodeStream(is);
				iv.setImageBitmap(bit);

				String path = Environment.getDataDirectory().getAbsolutePath();
				path += "/data/yell.exam01/files/test.png";

				Log.d("YELL", path);
				FileOutputStream fos = new FileOutputStream(path);
				bit.compress(Bitmap.CompressFormat.PNG, 100, fos);
			}
			catch (Exception e) {
				// TODO: handle exception
				Log.d("YELL", e.getMessage());
			}
		}
	}
}

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.