Android Studio中使用图片压缩

android图片压缩,大多数文章都是直接调用android自带的压缩功能,不能说不好,就是压缩效率不高。

如果你要上传图片,特别是多张图片。就会出现上传图片比较大的问题。

网上百度了下找到了2中比较好的压缩方法。

第一种:

Luban多图片压缩(他有单图片和多图片压缩功能)

https://github.com/shaohui10086/AdvancedLuban

开始的时候我没找到他的多图片压缩功能个,只有带图片压缩的API,只有这个地址的提供的是多图片压缩

但是我没有验证,主要是没有使用。

第二种:

Compressor图片压缩,压缩功能很不错,我用5M的图片压缩后变成200K,还是很给力的

https://github.com/zetbaitsu/Compressor

他的API比较简单,我来说下我的使用方法:

Compress Image File
compressedImageFile = Compressor.getDefault(this).compressToFile(actualImageFile);
Compress Image File to Bitmap
compressedImageBitmap = Compressor.getDefault(this).compressToBitmap(actualImageFile);

一个是压缩成File文件,一个是压缩成Bitmap像素文件。入参要传入File

    /**
     * uri转file
     */
    public static  File uriToFile(Activity activity,Uri uri){
        if ( null == uri ) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if ( scheme == null )
            data = uri.getPath();
        else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
            data = uri.getPath();
        } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
            Cursor cursor = activity.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
            if ( null != cursor ) {
                if ( cursor.moveToFirst() ) {
                    int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
                    if ( index > -1 ) {
                        data = cursor.getString( index );
                    }
                }
                cursor.close();
            }
        }
        File file = new File(data);
        return file;
    }

然后就可以开启自己的上传了。

转载于:https://my.oschina.net/u/1399599/blog/853395

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值