public void onActivityResult(int requestCode, int resultCode, Intent data);
如果 data不为空,则使用方法一;如果data为空,则使用方法二。
方法一:
Cursor cursor1 = getActivity().getContentResolver().query(uri, null, null, null, null);// 根据Uri从数据库中找
if (cursor1 != null) {
cursor1.moveToFirst();// 把游标移动到首位,因为这里的Uri是包含ID的所以是唯一的不需要循环找指向第一个就是了
String filePath = cursor1.getString(cursor1.getColumnIndex("_data"));// 获取图片路
String orientation = cursor1.getString(cursor1
.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));// 获取旋转的角度
cursor1.close();
if (filePath != null) {
Bitmap bitmap2 = BitmapFactory.decodeFile(filePath);//根据Path读取资源图片
int angle = 0;
if (orientation != null && !"".equals(orientation)) {
angle = Integer.parseInt(orientation);
}
if (angle != 0) {
// 下面的方法主要作用是把图片转一个角度,也可以放大缩小等
Matrix m = new Matrix();
int width = bitmap2.getWidth();
int height = bitmap2.getHeight();
m.setRotate(angle); // 旋转angle度
bitmap2 = Bitmap.createBitmap(bitmap2, 0, 0, width, height, m, true);// 从新生成图片
avatar.setImageBitmap(bitmap2);
imgfile = cameraFile;
ModifyInfo();
}
}
}
方法二:
Bitmap bitmap1 = ImageUtils.compressImageFromFile(cameraFile.getPath());
int d = readPictureDegree(cameraFile.getPath());
if(d > 0){
bitmap1 = rotaingImageView(d, bitmap1);
}
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
public static Bitmap rotaingImageView(int angle , Bitmap bitmap) {
//旋转图片 动作
Matrix matrix = new Matrix();;
matrix.postRotate(angle);
System.out.println("angle2=" + angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}