不难,但用的时候有时候突然会想不起来。。记录一下吧

原文地址请保留http://www.cnblogs.com/rossoneri/p/3995096.html

先加权限

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 1 private void saveCroppedImage(Bitmap bmp) { 2         File file = new File("/sdcard/myFolder"); 3         if (!file.exists()) 4             file.mkdir(); 5  6         file = new File("/sdcard/temp.jpg".trim()); 7         String fileName = file.getName(); 8         String mName = fileName.substring(0, fileName.lastIndexOf(".")); 9         String sName = fileName.substring(fileName.lastIndexOf("."));10 11         // /sdcard/myFolder/temp_cropped.jpg12         String newFilePath = "/sdcard/myFolder" + "/" + mName + "_cropped" + sName;13         file = new File(newFilePath);14         try {15             file.createNewFile();16             FileOutputStream fos = new FileOutputStream(file);17             bmp.compress(CompressFormat.JPEG, 50, fos);18             fos.flush();19             fos.close();20         } catch (IOException e) {21             // TODO Auto-generated catch block22             e.printStackTrace();23         }24 25     }

这里比较重要的方法就是

bmp.compress(CompressFormat.JPEG, 50, fos);

文档如下:

booleanandroid.graphics.Bitmap.compress(CompressFormatformat,int quality,OutputStreamstream)

Write a compressed version of the bitmap to the specified outputstream. If this returns true, the bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream(). Note: not all Formats support all bitmap configs directly, so it is possible that the returned bitmap from BitmapFactory could be in a different bitdepth, and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque pixels).

Parameters:

formatThe format of the compressed image

qualityHint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting

streamThe outputstream to write the compressed data.

Returns:

true if successfully compressed to the specified stream.

第一个参数有三种,源码如下

 1 /** 2      * Specifies the known formats a bitmap can be compressed into 3      */ 4     public enum CompressFormat { 5         JPEG    (0), 6         PNG     (1), 7         WEBP    (2); 8  9         CompressFormat(int nativeInt) {10             this.nativeInt = nativeInt;11         }12         final int nativeInt;13     }

因为转换的原始bitmap后缀有不同,比如我用的jpg文件,保存下来也是jpg文件。但用的是CompressFormat.JPEG做参数。

这个参数应该和原始文件类型有关,这是个知识点,日后研究研究,写个根据类型自动选择参数的方法。

附上几个链接:

android bitmap compress(图片压缩)

Android中文API(136) —— Bitmap

更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. NPM 和webpack 的基础使用
  3. Python技巧匿名函数、回调函数和高阶函数
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. python list.sort()根据多个关键字排序的方法实现
  6. Android中Log机制详解
  7. Android(安卓)实现截屏
  8. Android(安卓)NDK Cmake学习记录
  9. android逆向工程教程

随机推荐

  1. android progressBar 背景改变
  2. 编译android 源码
  3. Android(Java):边框
  4. android ui LinearLayout中按扭平行显示
  5. Android(安卓)内存笔记
  6. android firstslide
  7. android 官方SDK文档
  8. Android(安卓)常见广告库包名合集
  9. Android(安卓)Property System
  10. Android中GridView区中且行间距设置