android:由URL载入中ImageView
生活随笔
收集整理的這篇文章主要介紹了
android:由URL载入中ImageView
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
兩種方法:
方法一:
Bitmap bimage= getBitmapFromURL(bannerpath); image.setImageBitmap(bimage);public static Bitmap getBitmapFromURL(String src) {try {Log.e("src",src);URL url = new URL(src);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setDoInput(true);connection.connect();InputStream input = connection.getInputStream();Bitmap myBitmap = BitmapFactory.decodeStream(input);Log.e("Bitmap","returned");return myBitmap;} catch (IOException e) {e.printStackTrace();Log.e("Exception",e.getMessage());return null;}}或者:
?
public static Bitmap loadBitmap(String url) {Bitmap bitmap = null;InputStream in = null;BufferedOutputStream out = null;try {in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);copy(in, out);out.flush();final byte[] data = dataStream.toByteArray();BitmapFactory.Options options = new BitmapFactory.Options();//options.inSampleSize = 1;bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);} catch (IOException e) {Log.e(TAG, "Could not load Bitmap from: " + url);} finally {closeStream(in);closeStream(out);}return bitmap; }?
?
?
方法2
?
Drawable drawable = LoadImageFromWebOperations(bannerpath);image.setImageDrawable(drawable);private Drawable LoadImageFromWebOperations(String url){try{InputStream is = (InputStream) new URL(url).getContent();Drawable d = Drawable.createFromStream(is, "src name");return d;}catch (Exception e) {System.out.println("Exc="+e);return null;}}版權(quán)聲明:本文博客原創(chuàng)文章,博客,未經(jīng)同意,不得轉(zhuǎn)載。
轉(zhuǎn)載于:https://www.cnblogs.com/mengfanrong/p/4635540.html
總結(jié)
以上是生活随笔為你收集整理的android:由URL载入中ImageView的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: assign,copy,strong,w
- 下一篇: jQuery 插件-(初体验一)