Android Studio
(Android Studio) URL주소에서 이미지 가져와서 보여주기
SAFE
2016. 11. 14. 12:56
출처 : http://blog.bykenny.com/entry/%EC%9B%B9%EC%9D%B4%EB%AF%B8%EC%A7%80-%EC%9D%B4%EB%AF%B8%EC%A7%80%EB%B7%B0%EC%97%90-%EB%84%A3%EA%B8%B0
출처 : http://blog.bykenny.com/webeuro-buteo-imiji-gajyeoogi/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.io.InputStream; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.util.Log; import android.widget.ImageView; //웹에서 가져온 이미지 비트맵으로 리턴하는 클래스 public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public ImageDownloader(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(String... urls) { String url = urls[0]; Bitmap mIcon = null; try { InputStream in = new java.net.URL(url).openStream(); mIcon = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); } return mIcon; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } | cs |
ImageDownloader.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package ~~; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.widget.ImageView; import java.io.InputStream; /** * Created by Administrator on 2016-11-10. */ //웹에서 가져온 이미지 비트맵으로 리턴하는 클래스 public class ImageDownloader extends AsyncTask<String, Void, Bitmap> { ImageView bmImage; public ImageDownloader(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(String... urls) { String url = urls[0]; Bitmap mIcon = null; try { InputStream in = new java.net.URL(url).openStream(); mIcon = BitmapFactory.decodeStream(in); int imageWidth = mIcon.getWidth(); int imageHeight = mIcon.getHeight(); System.out.println("가로길이 : " + imageWidth); System.out.println("세로길이 : " + imageHeight); } catch (Exception e) { // Log.e("Error", e.getMessage()); } return mIcon; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } | cs |