调用图库意图并在onActivityResult()
Uri selectedImageUri = data.getData();
String filePath;
try {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,
projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
filePath = cursor.getString(column_index);;
} catch (Exception e) {
}
File imageFile = new File(filePath);
Bitmap bitmap = decodeFile(imageFile.getAbsolutePath());
imageView.setImageBitmap(bitmap);
但是imageView中的图像旋转了90度。我甚至将图像作为文件发送到Web服务器,并且也是旋转的。
所以,我尝试检查Exif的详细信息,
//getOutputMediaFile() just returns a filepath to store image in my app folder
String newFilePath = getOutputMediaFile();
//800 is the desired width and height
Bitmap photo = decodeSampledBitmapFromFile(filePath, 800, 800);
//reduce the size of the image and store it in a new file ie.. newFilePath
FileOutputStream out = new FileOutputStream(newFilePath);
photo.compress(Bitmap.CompressFormat.JPEG, 100, out);
ExifInterface exif = new ExifInterface(newFilePath);
int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
int rotationInDegrees = exifToDegrees(rotation);
//If rotation is required
if (rotation != 0) {
Matrix matrix = new Matrix();
matrix.preRotate(rotationInDegrees);
File imageFile = new File(newFilePath);
//get the same image
Bitmap bitmap = decodeFile(imageFile.getAbsolutePath());
//rotate it
Bitmap adjustedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
//and save in the same file
FileOutputStream out2 = new FileOutputStream(newFilePath);
adjustedBitmap.compress(Bitmap.CompressFormat.JPEG, 100,out2);
}
decodeSampledBitmapFromFile()
public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,
int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize, Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
int inSampleSize = 1;
if (height > reqHeight) {
inSampleSize = Math.round((float) height / (float) reqHeight);
}
int expectedWidth = width / inSampleSize;
if (expectedWidth > reqWidth) {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
options.inSampleSize = inSampleSize;
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
decodeFile()
private Bitmap decodeFile(String path) {
IMAGE_MAX_SIZE = 800;
Bitmap b = null;
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
File f = new File(path);
FileInputStream fis = new FileInputStream(f);
BitmapFactory.decodeStream(fis, null, o);
fis.close();
int scale = 1;
if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
scale = (int) Math.pow(
2,
(int) Math.round(Math.log(IMAGE_MAX_SIZE
/ (double) Math.max(o.outHeight, o.outWidth))
/ Math.log(0.5)));
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
fis = new FileInputStream(f);
b = BitmapFactory.decodeStream(fis, null, o2);
fis.close();
} catch (IOException e) {
return null;
}
return b;
}
代码永远不会进入if(rotation != 0f){}
。这意味着图像方向是正确的,那么为什么我得到的文件或我得到的位图被旋转。我错过了什么?