发布于 2016-07-12 02:07:16 | 186 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java函数式编程,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要介绍了java根据url抓取并生成缩略图的示例,需要的朋友可以参考下

java根据url抓取并生成缩略图


public static Bitmap loadImageFromUrl(String url, int sc) {
        URL m;
        InputStream i = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream out = null;
        byte isBuffer[] = new byte[1024];
        if (url == null)
            return null;
        try {
            m = new URL(url);
            i = (InputStream) m.getContent();

            bis = new BufferedInputStream(i, 1024 * 4);
            out = new ByteArrayOutputStream();
            int len = 0;
            while ((len = bis.read(isBuffer)) != -1) {
                out.write(isBuffer, 0, len);
            }
            out.close();
            bis.close();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (out == null)
            return null;
        byte[] data = out.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(data, 0, data.length, options);
        options.inJustDecodeBounds = false;
        int be = (int) (options.outHeight / (float) sc);
        if (be <= 0) {
            be = 1;
        } else if (be > 3) {
            be = 3;
        }
        options.inSampleSize = be;
        Bitmap bmp = null;
        try {
            bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options); // 返回缩略图
        } catch (OutOfMemoryError e) {
            // TODO: handle exception
            System.gc();
            bmp = null;
        }
        return bmp;
    }



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务