这里先重复温习一下上一篇,调用相册获取图片:

  1. /***
  2. * 这个是调用android内置的intent,来过滤图片文件 ,同时也可以过滤其他的
  3. */
  4. Intent intent = new Intent();
  5. intent.setType("image/*");
  6. intent.setAction(Intent.ACTION_GET_CONTENT);
  7. startActivityForResult(intent, 1);

获取选择的图片:

  1. if (resultCode == Activity.RESULT_OK) {
  2. Uri uri = data.getData();
  3. try {
  4. String[] pojo = { MediaStore.Images.Media.DATA };
  5.  
  6. Cursor cursor = managedQuery(uri, pojo, null, null, null);
  7. if (cursor != null) {
  8. ContentResolver cr = this.getContentResolver();
  9. int colunm_index = cursor
  10. .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  11. cursor.moveToFirst();
  12. String path = cursor.getString(colunm_index);
  13. /***
  14. * 这里加这样一个判断主要是为了第三方的软件选择,比如:使用第三方的文件管理器的话,你选择的文件就不一定是图片了,
  15. * 这样的话,我们判断文件的后缀名 如果是图片格式的话,那么才可以
  16. */
  17. if (path.endsWith("jpg") || path.endsWith("png")) {
  18. picPath = path;
  19. Bitmap bitmap = PictureUtil.getSmallBitmap(picPath,480,320,50);
  20. //
  21. imageShow.setImageBitmap(bitmap);
  22. //这里更新发布分享按键的可点出状态
  23. btnSubmit.setEnabled(true);
  24.  
  25. } else {
  26. alert();
  27. }
  28. } else {
  29. alert();
  30. }
  31. } catch (Exception e) {
  32. }
  33. }

压缩处理类:

  1. /**
  2. * @Description 调用系统拍照或进入图库中选择照片,再进行裁剪,压缩.
  3. * @author chq
  4. */
  5. public class PictureUtil {
  6. //加载并显示一副图像对内存使用情况有显著的影响,Android提供了一个名为BitmapFactory 的实用程序类,该程序提供了一系列的静态方法,允许通过各种来源加载Bitmap图像。针对我们的需求,将从文件加载图像,并在最初的活动中显示它。幸运的是,BitmapFactory中的可用方法将会调用BitmapFactory.Options类,这使得我们能够定义如何将Bitmap读入内存。具体而言,当加载图像时,可以设置BitmapFactory应该使用的采样大小。在BitmapFactory.Options中指定inSampleSize参数。例如,将inSampleSize
  7. //= 8时,产生一幅图的大小是原始大小的1/8。要注意的是首先应将BitmapFactoryOptions.inJustDecodeBounds变量设置为true,这将通知BitmapFactory类只需返回该图像的范围,而无需尝试解码图像本身。最后将BitmapFactory.Options.inJustDecodeBounds设置为false,最后对其进行真正的解码。
  8. /**
  9. *
  10. * @param picPath
  11. * @param reqWidth
  12. * @param reqHeight
  13. * @param compress
  14. * @return
  15. */
  16. public static Bitmap getSmallBitmap(String picPath,int reqWidth,int reqHeight,int compress) {
  17. final BitmapFactory.Options options = new BitmapFactory.Options();
  18. options.inJustDecodeBounds = true;
  19. BitmapFactory.decodeFile(picPath, options); //options中将获得图片一些信息
  20. options.inSampleSize = calulateInSampleSize(options, reqWidth, reqHeight);
  21. options.inJustDecodeBounds = false;
  22.  
  23. Bitmap bitmap = BitmapFactory.decodeFile(picPath, options);
  24. if (bitmap == null) {
  25. return null;
  26. }
  27. int degree = readPictureDegree(picPath);
  28. bitmap = rotateBitmap(bitmap, degree);
  29. ByteArrayOutputStream baos = null;
  30. try {
  31. baos = new ByteArrayOutputStream();
  32. //压缩图片质量
  33. bitmap.compress(Bitmap.CompressFormat.JPEG,compress, baos);
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. } finally {
  37. try {
  38. if(baos != null) {
  39. baos.close();
  40. }
  41. } catch (IOException e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. return bitmap;
  46. }
  47.  
  48. private static int calulateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight) {
  49. //Raw height and width of image
  50. final int height = options.outHeight;
  51. final int width = options.outWidth;
  52. int inSampleSize = 1;
  53.  
  54. if(height>reqHeight || width>reqWidth) {
  55. final int heightRatio = Math.round((float)height / (float)reqHeight);
  56. final int widthRatio = Math.round((float)width / (float)reqWidth);
  57. // Choose the smallest ratio as inSampleSize value, this will
  58. // guarantee
  59. // a final image with both dimensions larger than or equal to the
  60. // requested height and width.
  61. inSampleSize = heightRatio < widthRatio ? widthRatio : heightRatio;
  62. }
  63. return inSampleSize;
  64. }
  65.  
  66. /**
  67. * 读取图片旋转处理
  68. * @param path
  69. * @return
  70. */
  71. private static int readPictureDegree(String path) {
  72. int degree = 0;
  73. try {
  74. ExifInterface exifInterface = new ExifInterface(path);
  75. int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  76. switch (orientation) {
  77. case ExifInterface.ORIENTATION_ROTATE_90:
  78. degree = 90;
  79. break;
  80. case ExifInterface.ORIENTATION_ROTATE_180:
  81. degree = 180;
  82. break;
  83. case ExifInterface.ORIENTATION_ROTATE_270:
  84. degree = 270;
  85. break;
  86. default:
  87. break;
  88. }
  89. } catch (IOException e) {
  90. e.printStackTrace();
  91. }
  92. return degree;
  93. }
  94. /**
  95. * 图片旋转处理
  96. * @param bitmap
  97. * @param rotate
  98. * @return
  99. */
  100. private static Bitmap rotateBitmap(Bitmap bitmap,int rotate) {
  101. if(bitmap == null) {
  102. return null;
  103. }
  104. int w = bitmap.getWidth();
  105. int h = bitmap.getHeight();
  106. Matrix mtx = new Matrix();
  107. mtx.postRotate(rotate);
  108. return Bitmap.createBitmap(bitmap,0,0,w,h,mtx,true);
  109. }
  110. }

Android图片处理-图片压缩处理的更多相关文章

  1. Android中的图片压缩

    1.android中计算图片占用堆内存的kB大小跟图片本身的kB大小无关,而是根据图片的尺寸来计算的. 比如一张 480*320大小的图片占用的堆内存大小为: 480*320*4/1024=600kB ...

  2. Android仿微信高效压缩图片(libjpeg)

    用过ios手机的同学应该很明显感觉到,ios拍照1M的图片要比安卓拍照排出来的5M的图片还要清晰.这是为什么呢? 这得了解android底层是如何对图片进行处理的. 当时谷歌开发Android的时候, ...

  3. Android 编程下图片的内存优化

    1. 对图片本身进行操作 尽量不要使用 setImageBitmap.setImageResource. BitmapFactory.decodeResource 来设置一张大图,因为这些方法在完成 ...

  4. 转-android图片降低图片大小保持图片清晰的方法

    http://i.cnblogs.com/EditPosts.aspx?opt=1 android里面对于图片的处理一直是个比较烦人的问题,烦人之处在于一个不小心,就有可能造成OOM. 最近碰到一个关 ...

  5. Android加载图片OOM错误解决方式

    前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...

  6. android 加载图片oom若干方案小结

    本文根据网上提供的一些技术方案加上自己实际开发中遇到的情况小结. 众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视手机而定).一般我们可以通过获取当 ...

  7. Android开发笔记——图片缓存、手势及OOM分析

    把图片缓存.手势及OOM三个主题放在一起,是因为在Android应用开发过程中,这三个问题经常是联系在一起的.首先,预览大图需要支持手势缩放,旋转,平移等操作:其次,图片在本地需要进行缓存,避免频繁访 ...

  8. Android高效异步图片加载框架

    概述 Android高效异步图片加载框架:一个高效的异步加载显示的图片加载框架,同时具备图片压缩,缓存机制等特性. 详细 代码下载:http://www.demodashi.com/demo/1214 ...

  9. 【Android】内存卡图片读取器,图库app

    上一篇<[Android]读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示>(点击打开链接)在真机上測试非常有问题.常常遇到内存溢出.卡死的情况.由于如今真机上的内存上,2G ...

  10. iOS学习-压缩图片(改变图片的宽高)

    压缩图片,图片的大小与我们期望的宽高不一致时,我们可以将其处理为我们想要的宽高. 传入想要修改的图片,以及新的尺寸 -(UIImage*)imageWithImage:(UIImage*)image ...

随机推荐

  1. const in C++

    const关键字是C++中常用的类型修饰符,用法非常灵活,使用const将大大改善程序的健壮性. const的作用 1.  定义const常量: 比如: const int Max = 100; 2. ...

  2. Maven学习之 Settings

    虽然天天在用,但是没有系统的学习过,总觉得别扭. 只能用于Java项目. 约定: repository  翻译成 仓库 build 翻译成 构建 build system 翻译成 构建系统 build ...

  3. Faster RNNLM (HS/NCE) toolkit

    https://github.com/kjw0612/awesome-rnn Faster Recurrent Neural Network Language Modeling Toolkit wit ...

  4. Appium客户端

    Appium版本:1.5.3 Xcode有两个版本:Xcode8.1   Xcode7.2.1 iOS10以下只能用Xcode7.2.1 iOS10及以上可以用Xcode8.1   1.Appium客 ...

  5. 工作中遇到的小问题: 做弹幕从数据库取出东西均匀插入marquee中,

    function getFloatContent() { var method = 'GETFLOATCONTENT'; $.ajax({ url: 'api/zhenqiapi.php', data ...

  6. 苹果会在明后年推出13寸屏iPad吗?

    摘要:苹果推大屏iPad的传闻由来已久,近日有国外媒体再次撰文称,这种大屏iPad不仅是苹果Mac继任者,同时也是Surface的有利竞争者……这真的可能吗?这只是分析师的捕风捉影,还是真有这种可能? ...

  7. 【原】java环境变量配置&& jdk配置 && 各配置的意义

    本配置需要新建JAVA_HOME和classpath两个: JAVA_HOME 指明JDK安装路径.(在安装好java之后就该配置) classpath 为java加载类(class or lib)路 ...

  8. Web前台直接加载GIS格式数据分析

    本文以Flex直接加载Shp.DWG和MDB为例. 首先看一份现估测数据: 1)  加载Shp文件,目前直接由前台Flex代码完成: 图1 在ArcCatalog里面的Shp文件 图2 直接在前台加载 ...

  9. dom4j 学习总结

    Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platfo ...

  10. 编译nginx时提示undefined reference to 'pcre_free_study' 的问题及解决

    ./configure --add-module=../ngx_devel_kit-0.2.19/ --add-module=../lua-nginx-module-0.9.19/  --with-l ...