```java
public class BitmapFileSetting {
/**
* 三星、小米手机设置
* 三星、小米手机拍照要旋转
* @param filePath
* @throws IOException
*/
public static File samsungPhoneSetting(String filePath) throws IOException {
//根据图片判断要旋转多少角度
int bitmapDegree = getBitmapDegree(filePath);
//根据图片路径转bitmap
Bitmap bitMBitmap;
bitMBitmap = decodeFile(filePath);
if (bitMBitmap == null){
return null;
}
//旋转后的bitmap
Bitmap rotateBitmapByDegree = rotateBitmapByDegree(bitMBitmap, bitmapDegree);
File saveBitmapFile = saveBitmapFile(rotateBitmapByDegree, filePath);
return saveBitmapFile;
}
/**
* uri 转 File
* @param uri
* @return
*/
public File uriTurnFile(Uri uri, Activity activity){
if(uri == null){
return null;
}
File file = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor actualimagecursor = activity.managedQuery(uri, proj, null,
null, null);
int actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToFirst();
String img_path = actualimagecursor
.getString(actual_image_column_index);
file = new File(img_path);
return file;
}
/**
* 根据路径 转bitmap
* @param urlpath
* @return
*/
public static Bitmap getBitMBitmap(String urlpath) {
Bitmap map = null;
try {
URL url = new URL(urlpath);
URLConnection conn = url.openConnection();
conn.connect();
InputStream in;
in = conn.getInputStream();
map = BitmapFactory.decodeStream(in);
// TODO Auto-generated catch block
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
/**
* 把batmap 转file
* @param bitmap
* @param filepath
*/
public static File saveBitmapFile(Bitmap bitmap, String filepath){
File file=new File(filepath);//将要保存图片的路径
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
/**
* 读取图片的旋转的角度
*
* @param path 图片绝对路径
* @return 图片的旋转角度
* @throws IOException
*/
public static int getBitmapDegree(String path) throws IOException {
int degree = 0;
// 从指定路径下读取图片,并获取其EXIF信息
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;
}
return degree;
}
/**
* 将图片按照某个角度进行旋转
*
* @param bm 需要旋转的图片
* @param degree 旋转角度
* @return 旋转后的图片
*/
public static Bitmap rotateBitmapByDegree(Bitmap bm, int degree) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(degree);
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
if (returnBm == null) {
returnBm = bm;
}
if (bm != returnBm) {
bm.recycle();
}
return returnBm;
}
/**
* 根据 路径 得到 file 得到 bitmap
* @param filePath
* @return
* @throws IOException
*/
public static Bitmap decodeFile(String filePath) throws IOException{
Bitmap b = null;
int IMAGE_MAX_SIZE = 600;
File f = new File(filePath);
if (f == null){
return null;
}
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
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();
return b;
}
}
如果你很着急的话,就直接把这个类复制进去,然后在图片选择回调的方法里面调用:
var file=BitmapFileSetting.samsungPhoneSetting(it[0].path)
it[0].path=file.path
第一行的代码意思是获取把角度旋转回来的file文件,第二行是把旋转后的文件path赋值给最初选择的文件的路径(it[0]是我自己代码里面回调回来的file对象,你改成你的就行)