zoukankan      html  css  js  c++  java
  • android:由URL载入中ImageView

    两种方法:

    方法一:

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

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    java冒泡算法和选择排序法
    JDBC操作数据库,比如修改电商数据库中的分类的id,让各商品随机
    RF使用
    安装RF框架(基于Python)
    selenium+java的常使用的一些操作
    selenium的常用操作
    selenium的8种定位方式(java举例)
    服务管理
    Linux软件安装
    项目依赖
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4635540.html
Copyright © 2011-2022 走看看