問題:在安卓部分手機上從相冊獲取到圖片的uri,在顯示的時候會默認旋轉90度。(特別是若是這張照片拍攝的時候是豎着拍攝的)java
解決問題思路:獲取照片的旋轉角度,在顯示的時候再根據旋轉角度,旋轉一下照片。android
大坑:使用ExifInterface獲取不到任何照片的信息。(ExifInterface是用來顯示照片的相關信息包括:分辨率,旋轉方向,感光度、白平衡、拍攝的光圈、焦距、分辨率、相機品牌、型號、GPS等信息。)post
解決辦法注重點:ui
在獲取到uri以後不要進行任何的圖片壓縮操做!this
ExifInterface類使用com.android.support:exifinterface:28.0.0 ,不要使用Android自帶的,避免在高版本中報錯。code
特別重要的一點:要先根據uri將圖片複製出來(通常是複製到本身App應用裏面),再根據新的路徑讀取照片。對象
下面貼出相關代碼:圖片
/** * 打開相冊 */ public void openFilePhoto() { Activity activity = this.mActivity.get(); Fragment fragment = this.mFragment.get(); Intent intent = new Intent(); if (Build.VERSION.SDK_INT < 19) { intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); } else { intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); } if (fragment != null) { fragment.startActivityForResult(intent, OPEN_FILE_PHOTO_REQUEST); } else { activity.startActivityForResult(intent, OPEN_FILE_PHOTO_REQUEST); } } //uri轉成file public static File getFileFromUri(Uri uri, Context context) { if (uri == null) { return null; } switch (uri.getScheme()) { case "content": return getFileFromContentUri(uri, context); case "file": return new File(uri.getPath()); default: return null; } } //根據uri轉成file 是以content開頭 private static File getFileFromContentUri(Uri contentUri, Context context) { if (contentUri == null) { return null; } File file = null; String filePath; String fileName; String[] filePathColumn = {MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME}; ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = contentResolver.query(contentUri, filePathColumn, null, null, null); if (cursor != null) { cursor.moveToFirst(); filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0])); fileName = cursor.getString(cursor.getColumnIndex(filePathColumn[1])); cursor.close(); if (!TextUtils.isEmpty(filePath)) { file = new File(filePath); } if (!file.exists() || file.length() <= 0 || TextUtils.isEmpty(filePath)) { filePath = getPathFromInputStreamUri(context, contentUri, fileName); } if (!TextUtils.isEmpty(filePath)) { file = new File(filePath); } } return file; } /** * 用流拷貝文件一份到本身APP目錄下 * * @param context * @param uri * @param fileName * @return */ public static String getPathFromInputStreamUri(Context context, Uri uri, String fileName) { InputStream inputStream = null; String filePath = null; if (uri.getAuthority() != null) { try { inputStream = context.getContentResolver().openInputStream(uri); File file = createTemporalFileFrom(inputStream, fileName); filePath = file.getPath(); } catch (Exception e) { Log.e("FileUtils", e.toString()); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (Exception e) { Log.e("FileUtils", e.toString()); } } } return filePath; } private static File createTemporalFileFrom(InputStream inputStream, String fileName) throws IOException { File targetFile = null; if (inputStream != null) { int read; byte[] buffer = new byte[8 * 1024]; //本身定義拷貝文件路徑 targetFile = new File(Constant.DIR_ROOT, fileName); if (targetFile.exists()) { targetFile.delete(); } OutputStream outputStream = new FileOutputStream(targetFile); while ((read = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, read); } outputStream.flush(); try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return targetFile; } /** * 讀取照片旋轉角度 * * @param path 照片路徑 * @return 角度 */ public static int readPictureDegree(String path) { if (TextUtils.isEmpty(path)) { return 0; } 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; }