Android 图片压缩、Bitmap 与 字符串互相转换

来源:互联网 发布:剑三萝莉捏脸数据图 编辑:程序博客网 时间:2024/05/09 11:43

前言
在Android 开发 时,在UI上显示 图片时,如果用原图显示,就会占用内存,加载慢等情况。如果把图片压缩后再显示,会快很多。这样就用到了图片压缩方法。

图片压缩:

    /**     * 图片压缩     * @param filePath     * @return     */    public static Bitmap convertIconToBitmap(String filePath){        BitmapFactory.Options options = new BitmapFactory.Options();        options.inJustDecodeBounds = true;        BitmapFactory.decodeFile(filePath, options);        options.inSampleSize = calculateInSampleSize(options, 480, 800);        options.inJustDecodeBounds = false;        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);        if (bitmap ==null){            return null;        }        ByteArrayOutputStream bos = null;        try {            bos = new ByteArrayOutputStream();            bitmap.compress(Bitmap.CompressFormat.PNG, 40, bos);            bos.close();        } catch (Exception e) {            e.printStackTrace();        }        return bitmap;    }    /**     * 设置 压缩图片大小     * @param options     * @param reqWidth     * @param reqHeight     * @return     */    private static int calculateInSampleSize(BitmapFactory.Options options,                                             int reqWidth, int reqHeight) {        // Raw height and width of image        final int height = options.outHeight;        final int width = options.outWidth;        int inSampleSize = 1;        if (height > reqHeight || width > reqWidth) {            // Calculate ratios of height and width to requested height and            // width            final int heightRatio = Math.round((float) height                    / (float) reqHeight);            final int widthRatio = Math.round((float) width / (float) reqWidth);            // Choose the smallest ratio as inSampleSize value, this will            // guarantee            // a final image with both dimensions larger than or equal to the            // requested height and width.            inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;        }        return inSampleSize;    }

如果要实现 图片保存到数据库,就不能像保存到本地那样 直接保存。这时,需要先转换为字符串,再用字符串保存到数据库。在需要时,再从数据库读取,再转换为 Bitmap 到UI 显示。

代码如下:

    /**     * Bitmap convert to String.     * @param bitmap     * @return     */    public static String convertIconToString(Bitmap bitmap){        String bitmapStr = "";        ByteArrayOutputStream bos = new ByteArrayOutputStream();        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);         byte[] bytes = bos.toByteArray();        bitmapStr = Base64.encodeToString(bytes, Base64.DEFAULT);        return bitmapStr;    }    /**     * String convert to Bitmap.     * @param str     * @return     */    public static Bitmap convertStringToIcon(String str){        Bitmap bitmap = null;        byte[] bytes;        try{            bytes = Base64.decode(str, Base64.DEFAULT);            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);        }catch (Exception e){            e.printStackTrace();        }        return bitmap;    }

后记:这里只是实现了,Bitmap 与 字符串直接的互转。至于怎么 保存到数据库与读取操作就自己实现了哈o( ̄▽ ̄)d~

参考资料:
http://blog.csdn.net/luhuajcdd/article/details/8948261

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 买楼房70年以后怎么办 暂住证过期2个月怎么办 换领驾驶证超期了怎么办 车牌租出去要不回来怎么办 汽车放久了没电怎么办 上海网约车资格证怎么办 手机掉在滴滴上怎么办 在滴滴上丢手机怎么办 把东西落在出租车上怎么办 租好房子后悔了怎么办 转租房东不退押金怎么办 亲戚借户口本办公租房怎么办 7.1深圳禁行货车怎么办 怎么办无锡市的货车临时通行证 武汉医保卡密码忘了怎么办 打出租车被黑了怎么办 行李掉在出租车上怎么办 家庭农场买农机怎么办补贴 在异地买的保险怎么办 理发店被投诉工商局找我怎么办? 临安市民卡丢了怎么办 合肥分期付款买手机被骗怎么办 公司注销后还遇到投诉怎么办 超市购物结账时少收钱怎么办 卖给顾客东西时会有斜念怎么办 实体店家纺想换货怎么办 劳动仲裁裁决部分不服怎么办 劳动仲裁公司拒不履行怎么办? 苹果手机被黑客锁了怎么办 出租大面积厂房的中介费用怎么办 中山房子网签不了怎么办 物业服务太差该怎么办 取完公积金的卡怎么办 车卖了对方不过户怎么办 卫生间下水道堵了怎么办妙招 教练不让你练车怎么办 教练不让我练车怎么办 考三要练车教练不给练怎么办 科目二指纹打卡指纹不清楚怎么办 教练凶你的时候怎么办 人行聘用制3年后怎么办