当前位置: 移动技术网 > IT编程>开发语言>Java > java根据url抓取并生成缩略图的示例

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

2019年07月22日  | 移动技术网IT编程  | 我要评论

ume双井,动漫h吧,仪铭

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网