1

我在我的android應用程序中使用此代碼來啓動相機/畫廊以獲取圖像,並將其顯示到ImageView中。爲什麼我的Android應用程序中的圖像選擇器將自動將人像圖像轉換爲橫向圖像?

當用戶選擇具有橫向方向的圖像時,所有東西都可以正常工作,但是當用戶選擇縱向圖像時,圖像會旋轉90度顯示。 我不明白爲什麼..我正在使用Android 4.3在Galaxy S3上測試我的應用程序。

我注意到問題只發生在圖片被手機拍攝時..可能這是一個問題與S3?

這是我的代碼:

private void openImageIntent() { 
     // Camera. 
     System.gc(); 

     final List<Intent> cameraIntents = new ArrayList<Intent>(); 
     final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
     final PackageManager packageManager = getPackageManager(); 
     final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0); 
     for(ResolveInfo res : listCam) { 
      final String packageName = res.activityInfo.packageName; 
      final Intent intent = new Intent(captureIntent); 
      intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); 
      intent.setPackage(packageName); 
      cameraIntents.add(intent); 
     } 

     // Filesystem. 
     final Intent galleryIntent = new Intent(); 
     galleryIntent.setType("image/*"); 
     galleryIntent.setAction(Intent.ACTION_GET_CONTENT); 

     // Chooser of filesystem options. 
     Intent chooserIntent = Intent.createChooser(galleryIntent, "Scegli dove prelevare l'immagine"); 

     // Add the camera options. 
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); 
     //Log.i("sto lanciando il chooser","vado eh"); 
     startActivityForResult(chooserIntent, 4982); 
    } 


    private String selectedImagePath; 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     super.onActivityResult(requestCode, resultCode, data); 



     if (resultCode == RESULT_OK) { 
      if (requestCode == 4982) { 


       Uri selectedImageUri = data.getData(); 
       selectedImagePath = getPath(selectedImageUri); 
       //Log.i("immagine",selectedImagePath); 

       myImageView.setImageBitmap(decodeSampledBitmapFromFile(new File(selectedImagePath))); 

      } 
     } 

    } 
    public String getPath(Uri uri) { 
     String[] projection = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = managedQuery(uri, projection, null, null, null); 
     int column_index = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 


    public static Bitmap decodeSampledBitmapFromFile(File file) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(file.getAbsolutePath(), o); 

     //The new size we want to scale to 
     final int REQUIRED_SIZE=430; 

     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 

     return BitmapFactory.decodeFile(file.getAbsolutePath(), o2); 
     } 

回答

2

許多Android設備,如果設備在縱向舉行在拍攝照片時,不旋轉圖像變爲縱向。相反,他們將圖像存儲在橫向圖像中,並在圖像中放入一個EXIF標題,以告訴任何圖像查看器「嘿!請旋轉270度!」。很可能,那就是你遇到的情況,因爲我知道三星設備經常遇到這個問題。

+0

有沒有在Android編程中讀取EXIF數據並旋轉圖像的方法? – allemattio

+0

@AlessandroMattioli:如果你有這個文件,你可以[使用'ExifInterface'](http://developer.android.com/reference/android/media/ExifInterface.html)。我不知道我的頭頂上只是一個半隨機的「Uri」的好解決方案。 – CommonsWare

+0

http://stackoverflow.com/questions/19511610/camera-intent-auto-rotate-to-90-degree –