这就是对我有效的方法。
public void onImageSelected(){
Uri uri=null;
try {
Bitmap bitmap = handleSamplingAndRotationBitmap(this, uri);
byte[] bytes = getBytesFromBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
public Bitmap handleSamplingAndRotationBitmap(Context context, Uri selectedImage)
throws IOException {
Bitmap decoded = getImageSampleOutput(context,selectedImage);
decoded = rotateImageIfRequired(this, decoded, selectedImage);
return decoded;
}
public Bitmap getImageSampleOutput(Context context, Uri selectedImage) throws IOException {
int MAX_HEIGHT = 1200;
int MAX_WIDTH = 1600;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream imageStream = context.getContentResolver().openInputStream(selectedImage);
BitmapFactory.decodeStream(imageStream, null, options);
imageStream.close();
options.inSampleSize = calculateInSampleSize(options, MAX_WIDTH, MAX_HEIGHT);
options.inJustDecodeBounds = false;
imageStream = context.getContentResolver().openInputStream(selectedImage);
Bitmap img = BitmapFactory.decodeStream(imageStream, null, options);
Bitmap decoded = compressImage(img);
return decoded;
}
public Bitmap compressImage(Bitmap bitmap) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
return BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
}
return inSampleSize;
}
private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) {
int rotation = getRotation(context, selectedImage);
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.postRotate(rotation);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
} else {
return img;
}
}
private static int getRotation(Context context, Uri selectedImage) {
int rotation = 0;
ContentResolver content = context.getContentResolver();
Cursor mediaCursor = content.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{"orientation", "date_added"},
null, null, "date_added desc");
if (mediaCursor != null && mediaCursor.getCount() != 0) {
while (mediaCursor.moveToNext()) {
rotation = mediaCursor.getInt(0);
break;
}
}
mediaCursor.close();
return rotation;
}
public byte[] getBytesFromBitmap(Bitmap bmp){
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG,70,stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
在三星Galaxy S7上,我能够将大小缩小到300kb。