1

ギャラリーから画像を選択してImageViewに画像を表示しているとき、一部の画像は90度で自動回転します。

これを無効にするにはどうすればよいですか?

コード:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);     
    setContentView(R.layout.main);

    m_galleryIntent = new Intent();
    m_galleryIntent.setType("image/*");
    m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    m_ProfileImageView = (ImageView) findViewById(R.id.imageView1);

    m_ProfileImageView.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            startActivityForResult(Intent.createChooser(m_galleryIntent, "Select Picture"),1);                          
        }

    });
}


public Bitmap readBitmap(Uri selectedImage) 
{ 
    Bitmap bm = null; 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inSampleSize = 5; 
    AssetFileDescriptor fileDescriptor =null; 
    try 
    { 
        fileDescriptor = this.getContentResolver().openAssetFileDescriptor(selectedImage,"r"); 
    } 
    catch (FileNotFoundException e) 
    { 
        e.printStackTrace(); 
    } 
    finally
    { 
        try 
        { 
            bm = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options); 
            fileDescriptor.close(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
    } 
    return bm; 
} 

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (resultCode == RESULT_OK) 
    {
        if (requestCode == 1) 
        {

            try 
            {
                Uri imageURI = data.getData();
                bitmapFromFile = readBitmap(imageURI);          
                m_ProfileImageView.setImageBitmap(bitmapFromFile);

            } 
            catch (Exception e) 
            {                           
                e.printStackTrace();
            } 
        }
    }
}
4

1 に答える 1

4

この画像は自分で回転させる必要があります。コンテンツプロバイダーから画像の向きの値、具体的にはImages.Media.ORIENTATIONフィールドを読み取り、それに応じて回転させます。

この画像は回転して保存されます。回転角はメディアデータベースに保存されます。

public int getOrientation(Uri selectedImage) {
    int orientation = 0;
    final String[] projection = new String[]{MediaStore.Images.Media.ORIENTATION};      
    final Cursor cursor = context.getContentResolver().query(selectedImage, projection, null, null, null);
    if(cursor != null) {
        final int orientationColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
        if(cursor.moveToFirst()) {
            orientation = cursor.isNull(orientationColumnIndex) ? 0 : cursor.getInt(orientationColumnIndex);
        }
        cursor.close();
    }
    return orientation;
}

ImageView.setImageMatrix()たとえば、画像を回転させることができます。

于 2012-09-11T12:15:48.470 に答える