Android图片压缩处理
2016-01-15 16:18
417 查看
在Android APP开发中经常会有上传图片和上传用户头像的功能,但是我们知道,手机拍摄的照片大多都有2M - 5M,把这么大的图片上传上去是需要很长时间的,并且图片过大非常容易导致应用卡顿甚至是OOM直接崩溃掉,那么为了优化性能就必须要做图片压缩处理,在最近的项目中通过查阅资料自己封装了图片压缩处理的工具类,大家在开发中应该用的到。代码如下:
以上工具类可以完成(1)图片的压缩 (2)将压缩后的Bitmap保存到本地
然后就可以从本地拿到图片上传到服务器了。压缩后的图片上传的时间就会大大缩短,并且会避免程序的OOM。
/** * Author: lihongxiang * Time: 上午 11:38 * Email:lihongxiangleo@163.com */ public class CompressImageUtils { public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, 480, 800); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(filePath, options); int degree = readPictureDegree(filePath); bm = rotateBitmap(bm, degree); if (bm == null) { return null; } ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.JPEG, 30, baos); } finally { try { if (baos != null) baos.close(); } catch (IOException e) { e.printStackTrace(); } } return bm; } private static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } //处理图片旋转 private static Bitmap rotateBitmap(Bitmap bitmap, int rotate) { if (bitmap == null) return null; int w = bitmap.getWidth(); int h = bitmap.getHeight(); // Setting post rotate to 90 Matrix mtx = new Matrix(); mtx.postRotate(rotate); return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); } 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保存到本地 * * @param srcPath * @param mBitmap * @throws IOException */ public static void saveMyBitmap(String srcPath, Bitmap mBitmap) throws IOException { File f = new File(srcPath); f.createNewFile(); FileOutputStream fOut = null; try { fOut = new FileOutputStream(f); } catch (FileNotFoundException e) { e.printStackTrace(); } mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut); try { fOut.flush(); } catch (IOException e) { e.printStackTrace(); } try { fOut.close(); } catch (IOException e) { e.printStackTrace(); } } }
以上工具类可以完成(1)图片的压缩 (2)将压缩后的Bitmap保存到本地
然后就可以从本地拿到图片上传到服务器了。压缩后的图片上传的时间就会大大缩短,并且会避免程序的OOM。
相关文章推荐
- android studio 快捷键
- Android Activity启动模式之singleTop实例详解
- Android各种访问权限Permission详解
- Android Studio 调试-----Watting for Debugger
- Service Manager成为Android进程间通信(IPC)机制Binder守护进程之路
- Android 开发最佳实践
- 全面了解Android开发规范
- android JNI使用AAssetManager读取asset目录内容
- Android消息推送完美方案
- Android总结篇系列:Android Intent (含Android 5.0 新知识)
- android studio 调试----------Watting for processing
- Android开发艺术探索—Parcelable接口
- Android 5.0之后隐式声明Intent 启动Service引发的问题
- Android Material Design
- Android Activity启动模式之standard实例详解
- 通过硬件层提高Android动画的性能
- Android中比较常见的Java super关键字
- Android 程序打包及签名
- FFmpeg 2.8.4 移植到android平台(一)
- Android 末尾始终保持00.00的EditText