试试这个
public Bitmap compressImage(String imageUri) {
String filePath = getRealPathFromURI(imageUri);
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath,options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
ExifInterface exi;
int orien=0;
try {
exi = new ExifInterface(filePath);
orien = exi.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
} catch (IOException e1) {
e1.printStackTrace();
}
float maxHeight = ScreenHeight;
float maxWidth = ScreenWidth;
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight/actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth/actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
options.inSampleSize =calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16*1024];
try{
bmp = BitmapFactory.decodeFile(filePath,options);
}
catch(OutOfMemoryError exception){
}
try{
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
}
catch(OutOfMemoryError exception){
}
float ratioX = actualWidth/(float)options.outWidth;
float ratioY = actualHeight/(float)options.outHeight;
float middleX = actualWidth/2.0f;
float middleY = actualHeight/2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth()/2, middleY - bmp.getHeight()/2, new Paint(Paint.FILTER_BITMAP_FLAG));
boolean isPorait=true;
if(((Activity)context).getResources().getConfiguration().orientation==Configuration.ORIENTATION_LANDSCAPE){
isPorait=false;
}
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
if(isPorait)
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
if(!isPorait)
matrix.postRotate(180);
else
matrix.postRotate(-90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
if(isPorait)
matrix.postRotate(270);
else
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0,scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
} catch (IOException e) {
}
bmp.recycle();
return scaledBitmap;
}
1)请提供您尝试过的一些代码。 2)请做一些研究 3)你永远不能保证你会压缩那么多 – apmartin1991
在数据库上保存图像不是最好的解决方案。您可以将图像保存在内部存储器上,并将图像的路径保存到数据库中作为字符串。 –
是的,这是一个不错的选择。但我需要将图像字节发送到服务器,并且服务器接受大小小于500 KB的图像。 –