您的位置:首页 > 移动开发 > Android开发

android 图片压缩的方法

2012-09-10 08:48 357 查看
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

// 获取这个图片的宽和高
Bitmap bitmap = BitmapFactory.decodeFile(
mPicCursor.getString(3)	, options);// 此时返回bm为空
options.inJustDecodeBounds = false;
// 计算缩放比
int be = (int) (options.outHeight / (float) 200);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// 重新读入图片,注意这次要把options.inJustDecodeBounds设为false哦
bitmap = BitmapFactory.decodeFile(
mPicCursor.getString(3), options);
bitmap = Bitmap.createScaledBitmap(bitmap, 150, 160, false);//将图片设定为指定大小
try {
FileOutputStream out = new FileOutputStream(mUser
.getPhotoUrl());
if (bitmap.compress(Bitmap.CompressFormat.JPEG,
35, out)) {
out.flush();
out.close();
}
} catch (Exception e) {
}


2................................

该方法是从网上copy的,没有尝试

public static String getImageString(String imgFilePath){
Bitmap mBitmap=BitmapFactory.decodeFile(imgFilePath);
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap newBitmap=Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);

ByteArrayOutputStream out=new ByteArrayOutputStream();
newBitmap.compress(CompressFormat.JPEG, 100, out);
byte []bytes=out.toByteArray();
String imageString=Base64.encodeToString(bytes, Base64.DEFAULT);
return imageString;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: