禁用Android的图像自动旋转 [英] Disable Android image auto rotate

查看:325
本文介绍了禁用Android的图像自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我'请从库图像,并显示在图像中的ImageView一些图片被自动带旋转90度。

我如何禁用此?

code:

  @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    m_galleryIntent =新的Intent();
    m_galleryIntent.setType(图像/ *);
    m_galleryIntent.setAction(Intent.ACTION_GET_CONTENT);    m_ProfileImageView =(ImageView的)findViewById(R.id.imageView1);    m_ProfileImageView.setOnClickListener(新OnClickListener()
    {
        @覆盖
        公共无效的onClick(视图v)
        {
            startActivityForResult(Intent.createChooser(m_galleryIntent,选择图片),1);
        }    });
}
公共位图readBitmap(URI selectedImage)
{
    位图BM = NULL;
    BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inSampleSize = 5;
    AssetFileDescriptor文件描述符= NULL;
    尝试
    {
        文件描述符= this.getContentResolver()openAssetFileDescriptor(selectedImage,R)。
    }
    赶上(FileNotFoundException异常E)
    {
        e.printStackTrace();
    }
    最后
    {
        尝试
        {
            BM = BitmapFactory.de codeFileDescriptor(fileDescriptor.getFileDescriptor(),空,期权);
            fileDescriptor.close();
        }
        赶上(IOException异常E)
        {
            e.printStackTrace();
        }
    }
    返回BM;
}@覆盖
公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
    如果(结果code == RESULT_OK)
    {
        如果(要求code == 1)
        {            尝试
            {
                乌里imageURI = data.getData();
                bitmapFromFile = readBitmap(imageURI);
                m_ProfileImageView.setImageBitmap(bitmapFromFile);            }
            赶上(例外五)
            {
                e.printStackTrace();
            }
        }
    }
}


解决方案

您应该旋转自己这个图像。从内容提供商读取图像方向的价值,特别是Images.Media.ORIENTATION场及相应的旋转。

本的图像存储旋转。旋转角度被保存在媒体数据库。

 公众诠释getOrientation(URI selectedImage){
    INT方向= 0;
    最终的String [] =投影新的String [] {} MediaStore.Images.Media.ORIENTATION;
    最后光标光标= context.getContentResolver()查询(selectedImage,投影,NULL,NULL,NULL);
    如果(指针!= NULL){
        最终诠释orientationColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
        如果(cursor.moveToFirst()){
            方向= cursor.isNull(orientationColumnIndex)? 0:cursor.getInt(orientationColumnIndex);
        }
        cursor.close();
    }
    返回的方向;
}

您可以用图像旋转 ImageView.setImageMatrix()例如

When I'am selecting image from gallery and showing the image in ImageView some of the images is auto rotated with 90 degrees.

How do I disable this?

Code:

@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();
            } 
        }
    }
}

解决方案

You should rotate this images yourself. Read image orientation value from content provider, specifically Images.Media.ORIENTATION field and rotate it correspondingly.

This images are stored rotated. Rotate angle is saved in media database.

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;
}

You can rotate image using ImageView.setImageMatrix() for example.

这篇关于禁用Android的图像自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆