1. 质量压缩方法
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while ( baos.toByteArray().length / 1024>100) { //循环判断如果压缩后图片是否大于100kb,大于继续压缩
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
2. 图片按比例大小压缩方法
public static boolean getCacheImage(String filePath,String cachePath){
OutputStream out = null;
BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true; //设置为true,只读尺寸信息,不加载像素信息到内存
Bitmap bitmap = BitmapFactory.decodeFile(filePath, option); //此时bitmap为空
option.inJustDecodeBounds = false;
int bWidth = option.outWidth;
int bHeight= option.outHeight;
int toWidth = 400;
int toHeight = 800;
int be = 1; //be = 1代表不缩放
if(bWidth/toWidth>bHeight/toHeight&&bWidth>toWidth){
be = (int)bWidth/toWidth;
}else if(bWidth/toWidth<bheight bheight="">toHeight){
be = (int)bHeight/toHeight;
}
option.inSampleSize = be; //设置缩放比例
bitmap = BitmapFactory.decodeFile(filePath, option);
try {
out = new FileOutputStream(new File(cachePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap.compress(CompressFormat.JPEG, 100, out);
}
3. 正常情况下我们应该把两者相结合的
public static File scal(Uri fileUri){
String path = fileUri.getPath();
File outputFile = new File(path);
long fileSize = outputFile.length();
final long fileMaxSize = 200 * 1024;
if (fileSize >= fileMaxSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
int height = options.outHeight;
int width = options.outWidth;
double scale = Math.sqrt((float) fileSize / fileMaxSize);
options.outHeight = (int) (height / scale);
options.outWidth = (int) (width / scale);
options.inSampleSize = (int) (scale + 0.5);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
outputFile = new File(PhotoUtil.createImageFile().getPath());
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, fos);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d(, sss ok + outputFile.length());
if (!bitmap.isRecycled()) {
bitmap.recycle();
}else{
File tempFile = outputFile;
outputFile = new File(PhotoUtil.createImageFile().getPath());
PhotoUtil.copyFileUsingFileChannels(tempFile, outputFile);
}
}
return outputFile;
}
public static Uri createImageFile(){
// Create an image file name
String timeStamp = new SimpleDateFormat(yyyyMMdd_HHmmss).format(new Date());
String imageFileName = JPEG_ + timeStamp + _;
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = null;
try {
image = File.createTempFile(
imageFileName, /* prefix */
.jpg, /* suffix */
storageDir /* directory */
);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Save a file: path for use with ACTION_VIEW intents
return Uri.fromFile(image);
}
public static void copyFileUsingFileChannels(File source, File dest){
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} finally {
try {
inputChannel.close();
outputChannel.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}