2016-03-29 20 views
0

Seçilen görüntünün yönünü almak için aşağıdaki kodu kullanıyorum, böylece dikey olarak çekildiyse, galeriden seçildiğinde yatay olarak gösterilmeyecek.Resim galeriden seçildiğinde döndürülür

dosyasında hata oluştu imagePile = new File (selectedImage.toString()); selectedImage.toString() parametresinde, başlangıç ​​int dönüşünü değiştirdiğimde ve dikey bir görüntü seçtiğimde sonuç iyi oldu.

Parametreyi Dosyaya doğru muyum?

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    BitmapFactory.Options options; 
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) { 
     try { 
      options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      Uri selectedImage = data.getData(); 
      InputStream stream = getContentResolver().openInputStream(selectedImage); 
      Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options); 
      stream.close(); 
      //orientation 
      try { 
       int rotate = 0; 
       try { 
        File imageFile = new File(selectedImage.toString()); 
        ExifInterface exif = new ExifInterface(
          imageFile.getAbsolutePath()); 
        int orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 

        switch (orientation) { 
         case ExifInterface.ORIENTATION_ROTATE_270: 
          rotate = 270; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_180: 
          rotate = 180; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_90: 
          rotate = 90; 
          break; 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(rotate); 
       yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true); } 
      catch (Exception e) {} 
      //end of orientation 

      imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imgButton.setImageBitmap(yourSelectedImage); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show(); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
+0

görüntü bu

+0

Bu kod biraz yardımcı oldu mu? –

+0

Hayır, hatalar: android: configChanges ve screenOrientation orada izin verilmiyor – cgeekmt

cevap

1

böyle yönteminizi değiştirin:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    BitmapFactory.Options options; 
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) { 
     try { 
      options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      Uri selectedImage = data.getData(); 

      String[] filePath = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null); 
        cursor.moveToFirst(); 
      String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

      InputStream stream = getContentResolver().openInputStream(selectedImage); 
      Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options); 
      stream.close(); 
      //orientation 
      try { 
       int rotate = 0; 
       try { 
        ExifInterface exif = new ExifInterface(
          mImagePath); 
        int orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 

        switch (orientation) { 
         case ExifInterface.ORIENTATION_ROTATE_270: 
          rotate = 270; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_180: 
          rotate = 180; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_90: 
          rotate = 90; 
          break; 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(rotate); 
       yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true); } 
      catch (Exception e) {} 
      //end of orientation 

      imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imgButton.setImageBitmap(yourSelectedImage); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

Bu işe yaradı teşekkürler :) – cgeekmt

+0

Sevindim! Lütfen cevabımı oylayın. Teşekkürler! –

+0

Doğru bir cevap olarak işaretledim. İlk önce 15'e ihtiyacım olduğu için oy kullanamam. Picasso kullanarak – cgeekmt

0

görüntü işleme için picasso iyi kütüphane de önbellekten resim yüklemek ve uygulamanızın performansını artırabilir kolayca kullanarak görüntüleri işleyebilir kullanıyor derlemek Bu yüzden çok fazla kod yapmaktan kaçının ve inşa hattınıza aşağıdaki satırı ekleyin ve ardından kodunuzda picasso kullanın. = Isim android:

compile 'com.squareup.picasso:picasso:2.5.0' 
+0

teşekkürler sorunu çözmek değil, görüntü hala döndürülebilir – Curio

İlgili konular