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

原文地址请保留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. android webview 使用js加载json文件失败
  2. Android Studio中R文件丢失
  3. android Environment 常用方法(获取存储目录)
  4. 文件浏览器
  5. Android 获取控件宽高的3种方法
  6. Android中创建与几种解析xml的方法!
  7. Android当方法总数超过64K时(Android Studio)

随机推荐

  1. Android(安卓)Studio安装配置详细步骤(超
  2. Android(安卓)定位GPS的使用
  3. 2020年GitHub标星2.9K的Android基础——
  4. Android(安卓)style & Theme 再探析(一)
  5. Hi,腾讯WeTest限免开放Android(安卓)Oreo
  6. 微软现新型移动勒索软件,已对所有 Android
  7. android 详细架构
  8. 阿里、腾讯、百度、网易、美团Android面
  9. Android(安卓)利用ViewPager+GridView,仿
  10. 最近用android写的一个项目,开发心得