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

原文地址请保留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. Java语言基础(方法与数组)_DAY05

    1:函数(掌握)   (1)定义在类中,有特定功能的一段小程序,可以独立运行.    (2)函数的格式:       修饰符 返回值类型 函数名(形参类型 形式参数1,形参类型 形式参数2...)   ...

  2. JDK中ConcurrentHashMap效率测试

    比较HashMap HashTable 和ConcurrentHashMap的效率. 一般情况下,达到一定的数量之后JDK1.5之后提供的ConcurrentHashMap集合类的效率是前两者的3~4 ...

  3. MySQL笔记(6)---锁

    1.前言 我们都知道在并发的情况下,修改数据时需要添加锁,但是却对数据库锁的工作原理不甚理解,不知道锁的运行机制,也就对数据的安全性无法明白.本章记录MySQL中锁的相关知识. 2.什么是锁 锁是数据 ...

  4. wordpress获取文章所属分类

    1.获取全部分类 <?php foreach((get_the_category()) as $category){ echo $category->cat_name; } ?> 2 ...

  5. 07 - JavaSE之容器

    本章宗旨:1136 -- 1个图 1个类 3个知识点 6个接口 容器 J2SDK 所提供的容器 API 位于 java.util 包内. 容器 API 的类图如下: Collection 接口的子接口 ...

  6. mysql通信协议的半双工机制理解

    一.通信知识中的半双工概念 通信的方式分为:单工通信,半双工,全双工. 全双工的典型例子是:打电话.电话在接到声音的同时也会传递声音.在一个时刻,线路上允许两个方向上的数据传输.网卡也是双工模式.在接 ...

  7. JDK8简化if-else

    简化if-else 1234567891011 User user = ...if (user != null) { String userName = user.getUserName(); if ...

  8. Python3 中使用sys.argv详解

    #/usr/bin/env python #coding:utf-8 import sys # print(sys.argv[1]) def readFile(filename): "&qu ...

  9. java Multimap

    实现 { "a": [ , , ], "b": [ , ] } 当然, HashMap<String, List<Integer>> 是 ...

  10. WIN32控件使用系统样式

    定义如下即可: #ifdef _M_IX86 #pragma comment(linker,"/manifestdependency:\"type='win32' name='Mi ...