这几天犯了一个错误,初期想着甩锅给后台的,但是还好及时发现了是自身的问题~
产生问题:通过拍照或相册回传的照片,直接传入后台,在用于展示的时候,照片角度出现问题,均不规则的旋转了90度,从而导致体验效果变差!
问题思考:后台一般都是你怎么存的,它怎么给你拿出来!所以在这里就可以发现问题本身是由我们前端造成的!
覆盖范围:图片被旋转的情况并不是百分百出现在每个机型上,只是会出现在某一部分机型之上,但是本着兼容的原则,我们只有逐个处理
解决方法:找了大约五篇博文左右,我找到了最懒也是最简单有效的方法,主要来自此篇文章,之所以没有转载而且署名原创的原因是因为更全面记录自己遇到的问题、场景、思路~
关联文章:Android进阶之路 - 拍照与相册选图(解决6.0,7.0的权限问题与文件保护问题)
主要代码部分,已全局进行了注释~
我想不会有比我还笨的人,加油把~
/**
* 拍照
*/
private void takePhoto() {
//拍照时存储的照片路径,最好提为全局变量,因为成功回传后需要使用到~
String photoPath = Environment.getExternalStorageDirectory().getPath() + "/picture_dow_" + SystemClock.currentThreadTimeMillis() + ".jpg";
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(InfoActivity.this, "com.bakheet.garage.fileprovider", sdcardTempFile));
} else {
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(sdcardTempFile));
}
startActivityForResult(intent, 1);
}
/**
* 从相册选取照片
*/
private void selectFromGallery() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//拍照回传
case 1:
if (resultCode == RESULT_OK && sdcardTempFile.exists()) {
//主要通过PhotoBitmapUtils内的amendRotatePhoto方法保存我们拍照时的照片状态
String newPath = PhotoBitmapUtils.amendRotatePhoto(photoPath, this);
//此时的图片file就是正确的了~
final File newSdcardTempFile = new File(newPath);
//这个方法是用于传入后台的,可忽略!!!
uploadImage(newSdcardTempFile);
}
break;
//相册回传
case 2:
ContentResolver contentResolver = getContentResolver();
if (data != null) {
//图片的U
Uri uri = data.getData();
if (uri!=null){
//使用工具类获取绝对路径
String path = ToolUtil.getFilePathFromContentUri(uri, contentResolver);
if (resultCode == RESULT_OK && sdcardTempFile.exists()) {
//主要通过PhotoBitmapUtils内的amendRotatePhoto方法保存我们的照片状态
String newPath = PhotoBitmapUtils.amendRotatePhoto(photoPath, this);
//此时的图片file就是正确的了~
final File newSdcardTempFile = new File(newPath);
//这个方法是用于传入后台的,可忽略!!!
uploadImage(newSdcardTempFile);
}
}
}
break;
default:
break;
}
}
/**
* 集合一些图片工具
*
* Created by zhuwentao on 2016-07-22.
*/
public class PhotoBitmapUtils {
/**
* 存放拍摄图片的文件夹
*/
private static final String FILES_NAME = "/MyPhoto";
/**
* 获取的时间格式
*/
public static final String TIME_STYLE = "yyyyMMddHHmmss";
/**
* 图片种类
*/
public static final String IMAGE_TYPE = ".png";
// 防止实例化
private PhotoBitmapUtils() {
}
/**
* 获取手机可存储路径
*
* @param context 上下文
* @return 手机可存储路径
*/
private static String getPhoneRootPath(Context context) {
// 是否有SD卡
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
|| !Environment.isExternalStorageRemovable()) {
// 获取SD卡根目录
return context.getExternalCacheDir().getPath();
} else {
// 获取apk包下的缓存路径
return context.getCacheDir().getPath();
}
}
/**
* 使用当前系统时间作为上传图片的名称
*
* @return 存储的根路径+图片名称
*/
public static String getPhotoFileName(Context context) {
File file = new File(getPhoneRootPath(context) + FILES_NAME);
// 判断文件是否已经存在,不存在则创建
if (!file.exists()) {
file.mkdirs();
}
// 设置图片文件名称
SimpleDateFormat format = new SimpleDateFormat(TIME_STYLE, Locale.getDefault());
Date date = new Date(System.currentTimeMillis());
String time = format.format(date);
String photoName = "/" + time + IMAGE_TYPE;
return file + photoName;
}
/**
* 保存Bitmap图片在SD卡中
* 如果没有SD卡则存在手机中
*
* @param mbitmap 需要保存的Bitmap图片
* @return 保存成功时返回图片的路径,失败时返回null
*/
public static String savePhotoToSD(Bitmap mbitmap, Context context) {
FileOutputStream outStream = null;
String fileName = getPhotoFileName(context);
try {
outStream = new FileOutputStream(fileName);
// 把数据写入文件,100表示不压缩
mbitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
return fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
if (outStream != null) {
// 记得要关闭流!
outStream.close();
}
if (mbitmap != null) {
mbitmap.recycle();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 把原图按1/10的比例压缩
*
* @param path 原图的路径
* @return 压缩后的图片
*/
public static Bitmap getCompressPhoto(String path) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10; // 图片的大小设置为原来的十分之一
Bitmap bmp = BitmapFactory.decodeFile(path, options);
options = null;
return bmp;
}
/**
* 处理旋转后的图片
* @param originpath 原图路径
* @param context 上下文
* @return 返回修复完毕后的图片路径
*/
public static String amendRotatePhoto(String originpath, Context context) {
// 取得图片旋转角度
int angle = readPictureDegree(originpath);
// 把原图压缩后得到Bitmap对象
Bitmap bmp = getCompressPhoto(originpath);;
// 修复图片被旋转的角度
Bitmap bitmap = rotaingImageView(angle, bmp);
// 保存修复后的图片并返回保存后的图片路径
return savePhotoToSD(bitmap, context);
}
/**
* 读取照片旋转角度
*
* @param path 照片路径
* @return 角度
*/
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;
}
/**
* 旋转图片
* @param angle 被旋转角度
* @param bitmap 图片对象
* @return 旋转后的图片
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
Bitmap returnBm = null;
// 根据旋转角度,生成旋转矩阵
Matrix matrix = new Matrix();
matrix.postRotate(angle);
try {
// 将原始图片按照旋转矩阵进行旋转,并得到新的图片
returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
}
if (returnBm == null) {
returnBm = bitmap;
}
if (bitmap != returnBm) {
bitmap.recycle();
}
return returnBm;
}
}