2013-11-09 31 views
0

我有,我想表现出从URI的图片在手机Android的矩阵postRotation不起作用

我想根据它拍摄于方位转动我的图片的位图图像视图 - 但它不起作用,总是以相同的方向显示图片。

这是我的ImageView:

<!-- picture imageView --> 

<ImageView 
    android:id="@+id/fragment_new_picture_imageview_picture" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:contentDescription="@string/app_name" 
    android:scaleType="fitXY" /> 

这是我的矩阵功能:

/** 
* Get the new picture {@link Bitmap} according to picture's right orientation 
* @return the picture's {@link Bitmap} 
*/ 
public Bitmap getPictureBitmap() 
{ 
    //if there is a saved instance - return it 
    if (pictureBitmap != null) 
     return pictureBitmap; 
    //decode bitmap 
    Bitmap bitmap = BitmapFactory.decodeFile(path); 
    //rotate bitmap 
    Matrix matrix = new Matrix();    
    matrix.postRotate(orientation); 
    //create new rotated bitmap 
    bitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);  
    return bitmap;  
} 

这是我的活动是如何在清单中提到:

<activity 
    android:name="com.coapps.pico.background.BackgroundActivity" 
    android:screenOrientation="portrait" 
    android:theme="@style/MainActivityTheme" > 
</activity> 

方向是利用查询到Media.EXTERNAL_CONTENT_URI和获取Media_ORIENTATION领域给予

+0

在哪里以及如何设置'orientation'?调用'postRotate()'时它有什么价值? – Simon

+0

只为你使用scaleType =“矩阵”ImageView – pskink

+0

我从systen Image表中获取它。这和路径是从表...但问题是,无论我设置为方向它不会改变 –

回答

0

首先得到度方向

public static int getExifOrientation(String filepath) { 
       int degree = 0; 
       ExifInterface exif = null; 
       try { 
         exif = new ExifInterface(filepath); 
       } catch (IOException ex) { 
         ex.printStackTrace(); 
       } 
       if (exif != null) { 
         int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1); 
         if (orientation != -1) { 
           // We only recognise a subset of orientation tag values. 
           switch (orientation) { 
           case ExifInterface.ORIENTATION_ROTATE_90: 
             degree = 90; 
             break; 
           case ExifInterface.ORIENTATION_ROTATE_180: 
             degree = 180; 
             break; 
           case ExifInterface.ORIENTATION_ROTATE_270: 
             degree = 270; 
             break; 
           } 

         } 
       } 

       return degree; 
     } 

然后使用这个学位在你的代码改变旋转

Matrix matrix = new Matrix();    
    matrix.postRotate(orientation);// get from getExifOrientation() 
    //create new rotated bitmap 
    bitmap = Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
+2

不创建一个新的位图,使用scaleType =“矩阵”的ImageView – pskink

+0

也许我不清楚方向值给出..我查询Media.EXTERNAL_CONTENT_URI获取Media.ORIENTATION,它有一个值...无论我插入什么值 - 它不起作用 –