android:通过URL加载ImageView

简介:

iphone上实现很简单,一行代码:

imageView.image =[UIImage imageWithContentsOfURL:theURL];

android:

两种方法:

复制代码
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;
        }
    }
复制代码

or try it

 

复制代码
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;
         }
     }
复制代码

 

搞定!

 本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/05/24/2515913.html,如需转载请自行联系原作者

相关文章
|
6月前
|
API Android开发 数据安全/隐私保护
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
229 0
|
4月前
|
XML Java Android开发
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
Android Studio App开发中异步任务AsynTask与异步服务IntentService的讲解与实战(实现四大名著的加载进度条 附源码)
55 0
|
5月前
|
定位技术 Android开发
[√]Android webview的url scheme
[√]Android webview的url scheme
407 0
|
6月前
|
缓存
SAP Fiori Elements 应用加载时的 url 参数 sap-ui-xx-viewCache=false
SAP Fiori Elements 应用加载时的 url 参数 sap-ui-xx-viewCache=false
23 0
|
8月前
|
Android开发
#6,Android Studio Android 开发控件 显示图片 ImageView的使用
#6,Android Studio Android 开发控件 显示图片 ImageView的使用
|
8月前
|
网络协议 前端开发 JavaScript
一个浏览器从加载URL到页面展示出来,经过了哪些步骤?
一个浏览器从加载URL到页面展示出来,经过了哪些步骤?
|
8月前
|
Android开发
Android ImageView视图的七种图片缩放类型
Android ImageView视图的七种图片缩放类型
110 0
|
8月前
|
Android开发
Android ImageView 使用
Android ImageView 使用
60 0
|
8月前
|
Android开发
Android ImageView scaleType 属性详细介绍与使用
Android ImageView scaleType 属性详细介绍与使用
91 0
|
9月前
|
XML Java 数据处理
Android:RecyclerView封装,打造列表极简加载
此库的封装,除了刷新加载库使用了SmartRefreshLayout,其他的都是自己从0到1的开发,目前,自己已经在项目中使用,暂时没有出现任何问题,当然了,后续,也会不断的对其进行优化,增加一些其他的功能,希望有需要的小伙伴,长期关注。
233 0

热门文章

最新文章