因爲前面的博文中忽略了點內容,因此在這裏補上,下面內容就是解決拍照或者選擇圖片顯示的時候圖片旋轉了90度或者其餘度數問題,以便照片能夠正面顯示:具體以下:數據庫
首先直接看上面博文下的拍完照或者選完圖後處理部分:ide
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (resultCode) { case 1: if (data != null) { // 取得返回的Uri,基本上選擇照片的時候返回的是以Uri形式,可是在拍照中有得機子呢Uri是空的,因此要特別注意 Uri mImageCaptureUri = data.getData(); // 返回的Uri不爲空時,那麼圖片信息數據都會在Uri中得到。若是爲空,那麼咱們就進行下面的方式獲取 if (mImageCaptureUri != null) { setImage(mImageCaptureUri);// 根據Uri處理並顯示圖片 } } break; default: break; } }
第二:處理90度問題並顯示:this
private void setImage(Uri mImageCaptureUri) { // 無論是拍照仍是選擇圖片每張圖片都有在數據中存儲也存儲有對應旋轉角度orientation值 // 因此咱們在取出圖片是把角度值取出以便能正確的顯示圖片,沒有旋轉時的效果觀看 ContentResolver cr = this.getContentResolver(); Cursor cursor = cr.query(mImageCaptureUri, null, null, null, null);// 根據Uri從數據庫中找 if (cursor != null) { cursor.moveToFirst();// 把遊標移動到首位,由於這裏的Uri是包含ID的因此是惟一的不須要循環找指向第一個就是了 String filePath = cursor.getString(cursor.getColumnIndex("_data"));// 獲取圖片路 String orientation = cursor.getString(cursor .getColumnIndex("orientation"));// 獲取旋轉的角度 cursor.close(); if (filePath != null) { Bitmap bitmap = 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 = bitmap.getWidth(); int height = bitmap.getHeight(); m.setRotate(angle); // 旋轉angle度 bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true);// 重新生成圖片 } photo.setImageBitmap(bitmap); } } }
OK完成,須要拍照和選擇圖片功能的部分請看 http://104zz.iteye.com/blog/1687662code