note

Access Web Image 본문

Android/기본

Access Web Image

투한 2012. 1. 13. 16:04

권한설정 >INTERNET해야됩니다



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <Button
            android:id="@+id/do_action"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Go!" />
        <TextView
            android:id="@+id/text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Press button for action" />            
<ImageView 
     android:id="@+id/ImageView01" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content">
</ImageView>
</LinearLayout>


package kr.android.webimage;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class AccessWebImage extends Activity {
	private static final String TAG="AccessWebImage";
	TextView textView;
	ImageView imageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        textView = (TextView)findViewById(R.id.text);
        imageView = (ImageView)findViewById(R.id.ImageView01);
        
        Button goButton =(Button)findViewById(R.id.do_action);
        goButton.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {		
				try{
					URL Text = new URL("http://postfiles9.naver.net/20110412_24/jms8641_13025950287563LdPD_JPEG/%BE%C6%C0%CC%C0%AF1.JPG?type=w1");
					
					Bitmap img = getRemoteImage(Text);
					imageView.setImageBitmap(img);
				}catch(Exception e){
					Log.e(TAG, "Erro in network call",e);
				}
			}
		});
    }
    private Bitmap getRemoteImage(URL url){
    	Bitmap bitmap = null;
    	try{
    		//지정한 URL을 통해서 InputStream(URL의 이미지 정보) 생성 
    		BufferedInputStream bis=
    				new BufferedInputStream(url.openStream());
    		//InputStream -> Bitmap
    		bitmap=BitmapFactory.decodeStream(bis);
    		//닫기
    		bis.close();
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    	return bitmap;
    }
}


'Android > 기본' 카테고리의 다른 글

SQLite  (0) 2012.01.13
SD CARD (SD 카드에 저장하기)  (0) 2012.01.13
Read JSON  (0) 2012.01.13
XML Resource  (0) 2012.01.13
StaticFile  (0) 2012.01.12