2017-04-12 16 views
0

ギャラリーから写真を選んだり、カメラから写真を撮ったり、ImageViewにセットすると回転していますが、どうやって回転しなくてもいいですか? ImageViewのに画像を設定するギャラリーから写真を選んだり、カメラから写真を撮ったりするときに写真が回転していますか?

public void setNewImage() { 

    new android.app.AlertDialog.Builder(getActivity()) 

      .setPositiveButton("camera", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        getActivity().startActivityForResult(takePicture, 0); 
       } 
      }) 

      .setNegativeButton("gallery", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Intent pickPhoto = new Intent(Intent.ACTION_PICK, 
          MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
        getActivity().startActivityForResult(pickPhoto, 1); 
       } 
      }) 

      .show(); 
} 

、ここでイム:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
     case 0: 
      if (resultCode == RESULT_OK && data != null && data.getData() != null) { 
       Uri filePath = data.getData(); 
       try { 
        //Getting the Bitmap from Gallery 
        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 

        //Setting the Bitmap to ImageView 
        NewpostFragment.post_image.setImageBitmap(bitmap); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       /* Uri picUri = data.getData(); 
       filePath = getPath(picUri); 
       img.setImageURI(picUri);*/ 

      } 

      break; 

     case 1: 
      if (resultCode == RESULT_OK && data != null && data.getData() != null) { 
       /*Uri picUri = data.getData(); 

       filePath = getPath(picUri); 

       img.setImageURI(picUri);*/ 



       Uri filePath = data.getData(); 
       try { 
        //Getting the Bitmap from Gallery 
        bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath); 
        //Setting the Bitmap to ImageView 
        NewpostFragment.post_image.setImageBitmap(bitmap); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 

      break; 
    } 
} 

答えて

0

まず、ACTION_IMAGE_CAPTUREonActivityResult()getData()を経由してあなたのUriを与えることはありません。バグのあるカメラアプリではそうするかもしれないが、ほとんどはそうしないだろう。選択肢は次のとおりです。EXTRA_OUTPUTを提供しないでください

  • 写真はあなたがEXTRA_OUTPUTに入れUriによって識別された場所に格納する必要があり、その場合にはACTION_IMAGE_CAPTUREIntentEXTRA_OUTPUT、または

  • を提供し、そして、0を使用するためのthis sample appを参照してくださいカメラアプリ

からサムネイルを取得するためにgetParcelableExtra("data")を使用はEXTRA_OUTPUTです。あなたがEXTRA_OUTPUT道を行けば

向きの面では、あなたがuse android.support.media.ExifInterface写真の向きを見つけることができ、その後、一致する画像を回転させるために何かをする(例えば、回転ImageView)。

ExifInterfaceandroid.support.media.ExifInterfaceとは異なる実装を使用していますが)を使用している場合は、this sample appを参照してください。

関連する問題