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

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

先加权限

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 private void saveCroppedImage(Bitmap bmp) {
File file = new File("/sdcard/myFolder");
if (!file.exists())
file.mkdir(); file = new File("/sdcard/temp.jpg".trim());
String fileName = file.getName();
String mName = fileName.substring(0, fileName.lastIndexOf("."));
String sName = fileName.substring(fileName.lastIndexOf(".")); // /sdcard/myFolder/temp_cropped.jpg
String newFilePath = "/sdcard/myFolder" + "/" + mName + "_cropped" + sName;
file = new File(newFilePath);
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(CompressFormat.JPEG, 50, fos);
fos.flush();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }

这里比较重要的方法就是

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.

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

 /**
* Specifies the known formats a bitmap can be compressed into
*/
public enum CompressFormat {
JPEG (0),
PNG (1),
WEBP (2); CompressFormat(int nativeInt) {
this.nativeInt = nativeInt;
}
final int nativeInt;
}

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

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

附上几个链接:

android bitmap compress(图片压缩)

Android中文API(136) —— Bitmap

[Android] 压缩图片并保存的更多相关文章

  1. Android --------- 压缩图片的尺寸和大小

    压缩图片大小,尺寸不变 将已知路径的图片压缩至不大于目标大小,并保存至指定路径 /** * 质量压缩,通过给定的路径来压缩图片并保存到指定路径 * * @param srcPath * 资源图片的路径 ...

  2. Android压缩图片到100K以下并保持不失真的高效方法

    前言:目前一般手机的相机都能达到800万像素,像我的Galaxy Nexus才500万像素,拍摄的照片也有1.5M左右.这么大的照片上传到服务器,不仅浪费流量,同时还浪费时间. 在开发Android企 ...

  3. android把图片 视频 保存到相册

    //android把图片文件添加到相册 ContentResolver localContentResolver = getContentResolver(); ContentValues local ...

  4. android 压缩图片大小,防止OOM

    android开发中,图片的处理是非常普遍的,经常是需要将用户选择的图片上传到服务器,但是现在手机的分辨率越来越好了,随便一张照片都是2M或以上,如果直接显示到ImageView中,是会出现OOM的, ...

  5. Xamarin.Android 压缩图片并上传到WebServices

    随着手机的拍照像素越来越高,导致图片赞的容量越来越大,如果上传多张图片不进行压缩.质量处理很容易出现OOM内存泄漏问题. 最近做了一个项目,向webservices上传多张照片,但是项目部署出来就会出 ...

  6. android -------- 压缩图片文件工具类

    项目中常常遇到文件压缩问题,上传文件大小限制 今天简单的分享一点干货,文件压缩,图片压缩,压缩Bitmap 主要通过尺寸压缩和质量压缩,以达到清晰度最优 效果图 源码地址: https://githu ...

  7. Android 下压缩图片—微弱失真

    Android下压缩图片的方法: 大概能将3M左右的图片压缩到100K左右, 几乎不失真. 代码如下: import java.io.FileNotFoundException; import jav ...

  8. Android笔记之 文件保存、压缩与清空删除

    这两天改进优化项目中图片上传的代码.考虑到可能有7.8M的比較大的图片,由于要先进行压缩.所以设计到文件的压缩,保存与清空删除操作. 在这里记下笔记. /** * 压缩并另存为,每次先清空再保存 */ ...

  9. Android 中图片压缩分析(上)

    作者: shawnzhao,QQ音乐技术团队一员 一.前言 在 Android 中进行图片压缩是非常常见的开发场景,主要的压缩方法有两种:其一是质量压缩,其二是下采样压缩. 前者是在不改变图片尺寸的情 ...

随机推荐

  1. mac下安装rzsz

    1.先安装item2,item2 市类似mac风格的终端 item2 下载地址,http://iterm2.com/downloads.html,下载后解压缩就能运行 2.Install Homebr ...

  2. TensorFlow.js之安装与核心概念

    TensorFlow.js是通过WebGL加速.基于浏览器的机器学习js框架.通过tensorflow.js,我们可以在浏览器中开发机器学习.运行现有的模型或者重新训练现有的模型. 一.安装     ...

  3. 静态编译 Qt5.7.0 (含 openssl 支持)

    关于Qt静态便宜的环境等,请先参见 Win10 + VS2015 下编译 Qt5.6.0 . 首先编译 openssl .我这里用的版本是 openssl 1.0.2j (新的1.1版本的便宜稍有不同 ...

  4. Java实现Socket通信

    一对一通信: 服务器端: public class ServerDemo { public static void main(String[] args) { ServerSocket serverS ...

  5. Disruptor入门

    一.什么是 Disruptor Disruptor是一个高性能的异步处理框架,或者可以认为是最快的消息框架(轻量的JMS),也可以认为是一个观察者模式实现,或者事件-监听模式的实现,直接称disrup ...

  6. 浅尝Vue.js组件(一)

    本篇目录: 组件名 组件注册 全局注册 基础组件的自动化全局注册 局部注册 在模块系统中局部注册 Prop 单向数据流 Prop验证 类型检查 非Prop特性 替换/合并已有的特性 禁用特性继承 自定 ...

  7. 常用的Maven 插件

    Maven本质上是一个插件框架,它的核心并不执行任何具体的构建任务,所有这些任务都交给插件来完成. 例如编译源代码是由maven- compiler-plugin完成的.进一步说,每个任务对应了一个插 ...

  8. tomcat配置说明,配置不用访问工程名

    # 配置项目访问不用输入项目名称 # [重要]亲测 <Host>中的 appBass="" 一定不能带目录,必须为空,因为启动tomcat会启动appBass下面的所有 ...

  9. NAT介绍

    NAT 即网络地址转换NAT作用:实现内网IP地址和公网IP地址之间的转换可以有效地缓解IP地址危机可以隐藏内网地址实现负载均衡实现内网和内网之间的通信 NAT分类:分类静态NAT:将内网IP地址一对 ...

  10. elasticSearch6源码分析(3)cluster模块

    1. cluser概述 One of the main roles of the master is to decide which shards to allocate to which nodes ...