본문 바로가기

구글과인터넷/안드로이드

안드로이드 웹을 이용한 이미지 관련

출처: http://kpbird.blogspot.com/2011/05/android-loading-image-from-server.html


Hello Guys,


Here I am writing easiest way to download image from server. Following function return Drawable object.  Use drawable object as per your  requirement.


01private Drawable LoadImageFromWeb(String url) {
02  try {
03   InputStream is = (InputStream) new URL(url).getContent();
04   Drawable d = Drawable.createFromStream(is, "src name");
05   return d;
06  catch (Exception e) {
07   System.out.println("Exc=" + e);
08   return null;
09  }
10 }

//////////////////////////////////////////////////////////////////////////////////////////////////////

출처: http://blog.daum.net/malcolmx/18109327

<웹의 이미지를 어떻게 받아서 화면에 출력할 것인가?>
http://android-apps-blog.blogspot.com/2011/04/how-to-load-image-from-web-on-android.html


<Android Loading Image from Server> / Ketan Parmar
http://kpbird.blogspot.com/2011/05/android-loading-image-from-server.html
- 가장 간결한 원격 이미지 로드 코드를 소개

<웹에서 특정 이미지를 받아서 안드로이드의 전체화면으로 출력하는 액티비티 예제>
- /res/layout/image.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView android:layout_gravity="center|center_vertical|center_horizontal"
        android:id="@+id/imageView" android:src="@drawable/icon"
        android:layout_width="match_parent" android:layout_height="match_parent"></ImageView>
</LinearLayout>

- /src/.../Image.java
public class Image extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image);
        Context context = this.getBaseContext();
        Drawable image = ImageOperations(context, "http://res.heraldm.com/content/image/2010/06/01/20100601000273_1.jpg", "image.jpg");
        ImageView imgView = new ImageView(context);
        imgView = (ImageView) findViewById(R.id.imageView);
        imgView.setImageDrawable(image);
    }

    private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
        try {
            URL imageUrl = new URL(url);
            InputStream is = (InputStream) imageUrl.getContent();
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}