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;
}
然后就可以开启自己的上传了。