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;
             }
         }

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

  • 相关阅读:
    UIWebView控件中 字体大小和字体样式的修改
    IOS statusBarStyle 设置
    SSZipArchive解压失败的原因
    uiimageview 的 animation 动画
    App网络管理
    系统日志输出工具类
    软键盘管理
    获取App应用信息
    Activity管理类
    SharePreference工具类
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4635540.html
Copyright © 2011-2022 走看看