android 获取Bitmap缩略图

/**
     * 根据宽度从本地图片路径获取该图片的缩略图
     * 
     * @param localImagePath
     *            本地图片的路径
     * @param width
     *            缩略图的宽
     * @param addedScaling
     *            额外可以加的缩放比例
     * @return bitmap 指定宽高的缩略图
     */
    public static Bitmap getBitmapByWidth(String localImagePath, int width,
            int addedScaling)
    {
        if (TextUtils.isEmpty(localImagePath))
        {
            return null;
        }
        
        Bitmap temBitmap = null;
        
        try
        {
            BitmapFactory.Options outOptions = new BitmapFactory.Options();
            
            // 设置该属性为true,不加载图片到内存,只返回图片的宽高到options中。
            outOptions.inJustDecodeBounds = true;
            
            // 加载获取图片的宽高
            BitmapFactory.decodeFile(localImagePath, outOptions);
            
            int height = outOptions.outHeight;
            
            if (outOptions.outWidth > width)
            {
                // 根据宽设置缩放比例
                outOptions.inSampleSize = outOptions.outWidth / width + 1
                        + addedScaling;
                outOptions.outWidth = width;
                
                // 计算缩放后的高度
                height = outOptions.outHeight / outOptions.inSampleSize;
                outOptions.outHeight = height;
            }
            
            // 重新设置该属性为false,加载图片返回
            outOptions.inJustDecodeBounds = false;
            outOptions.inPurgeable = true;
            outOptions.inInputShareable = true;
            temBitmap = BitmapFactory.decodeFile(localImagePath, outOptions);
        }
        catch (Throwable t)
        {
            t.printStackTrace();
        }
        
        return temBitmap;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值