|
/**
* 通过ExifInterface类读取图片文件的被旋转角度,并且纠正。
* @param context
* @param u 图片文件的路径
* @return
*/
public static Bitmap getExifOrientation(Context context, Uri u) {
Bitmap bm = null;
//不管是拍照还是选择图片每张图片都有在数据中存储也存储有对应旋转角度orientation值
Cursor cursor = context.getContentResolver().query(u, null, null, null, null);//根据Uri从数据库中查找
if (cursor != null && cursor.moveToNext()) {
String filePath = cursor.getString(cursor.getColumnIndex("_data"));//获取图片路径
String orientation = cursor.getString(cursor.getColumnIndex("orientation"));//获取图片旋转的角度
cursor.close();
if (filePath != null) {
try {
Bitmap bitmap = Utils.comp(BitmapFactory.decodeFile(filePath),MainCompileActivity.width,MainCompileActivity.height);//根据Path读取资源图片
int angle = 0;//角度
if (orientation != null && !"".equals(orientation)) {
angle = Integer.parseInt(orientation);
}
if (angle != 0) {
//纠正图片的角度
final Matrix m = new Matrix();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
m.setRotate(angle);
bm = Bitmap.createBitmap(bitmap, 0, 0, width, height,m, true);
} else {
bm = bitmap;
}
} catch (Exception e) {
bm = null;
Log.e("===>>>", "抛出异常");
}
}else{
bm = null;
}
}else{
bm = null;
}
return bm;
}
|
|
![]() 10分 |
一般情况下出现在哪个位置?
|
![]() |
这个我也不清楚,因为上面那个方法是我修改过的了,行数都已经乱了。大概是if (angle != 0) { 这里 } |
![]() |
。。。不会吧 一个判断就oom了?
|
![]() |
Bitmap bitmap = Utils.comp(BitmapFactory.decodeFile(filePath),MainCompileActivity.width,MainCompileActivity.height);
这个地方通过BitmapFactory.decodeFile(filePath)这个方法,当图片稍微大一点的时候,加载出来的bitmap就非常大,消耗大量的内存,然后接下来你对这个bitmap做旋转,内存肯定溢出了,建议你查看下bitmap的大小,看是不是这个问题。如果是的话,那就改变下获取bitmap的方式或者加个Option |
![]() |
那这里应该怎么改呀? |
![]() 30分 |
给你一个方法,你试试,不行的话我还有一个,哈哈哈 |

