android 图片压缩的方法

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;
  }


你可能感兴趣的:(android,exception,String,float,byte,Matrix)