2014-06-16 37 views
1

我想設置使用Intent.ACTION_GET_CONTENT視圖背景圖像後改變和處理onActivityResult方法...形象定位得到選擇

public void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK && requestCode == SELECT_PICTURE) { 
     Uri selectedImageUri = data.getData();   
     try { 
      InputStream inputStream = 
       getContentResolver().openInputStream(selectedImageUri); 
      Drawable drawable = Drawable.createFromStream(inputStream,  
       selectedImageUri.toString()); 
      mView.setBackground(drawable); 
     } catch(FileNotFoundException e) {} 
    } 
} 

它正常工作,從GALLARY一些圖片,但並不適用於所有圖片。選擇某些圖像後,圖像方向會發生變化。

+0

默認圖像取向在許多手機三星特別景觀。在顯示圖像之前,您需要檢查圖像中的exif旋轉標籤 –

+0

請看我的答案:http://stackoverflow.com/questions/14066038/why-image-captured-using-camera-intent-gets-rotated-on -some-devices-in-android –

回答

4

試試下面的代碼: -

try { 
     File f = new File(imagePath); 
     ExifInterface exif = new ExifInterface(f.getPath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 

     int angle = 0; 

     if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      angle = 90; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
      angle = 180; 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      angle = 270; 
     } 

     Matrix mat = new Matrix(); 
     mat.postRotate(angle); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 2; 

     Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), 
       null, options); 
     bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), 
       bmp.getHeight(), mat, true); 
     ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.PNG, 100, 
       outstudentstreamOutputStream); 
     imageView.setImageBitmap(bitmap); 

    } catch (IOException e) { 
     Log.w("TAG", "-- Error in setting image"); 
    } catch (OutOfMemoryError oom) { 
     Log.w("TAG", "-- OOM Error in setting image"); 
    } 
+0

感謝@Golu的快速響應,對我的工作很好:) –