zoukankan      html  css  js  c++  java
  • [Android] 压缩图片并保存

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

    原文地址请保留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.jpg
    12         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 block
    22             e.printStackTrace();
    23         }
    24 
    25     }

     这里比较重要的方法就是

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

    文档如下:

    boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream)

    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:

    format The format of the compressed image

    quality Hint 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

    stream The 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

  • 相关阅读:
    python 打印出水仙花数
    pycharm 2020 激活码 破解教程
    Python 封装一个函数,查找文字字符串数字英文下标
    Python 分解质因数
    python 封装一个取符串长度的函数
    Python 正整数相加其余忽略
    Python 输入字符串找(String)下标 没有返回-1
    CPU 和 GPU 的区别
    Deferred Shading,延迟渲染(提高渲染效率,减少多余光照计算)【转】
    BumpMap、NormalMap的区别
  • 原文地址:https://www.cnblogs.com/rossoneri/p/3995096.html
Copyright © 2011-2022 走看看