如何查看从图库中选择的图像的方向

时间:2015-11-03 20:34:52

标签: android image

对于我的应用,我需要能够找到用户从照片库中选择的图像的方向。这源于一些设备(如三星手机)将图像保存为景观;另一方面,Nexus设备另存为肖像。目前,在我的应用程序中,如果用户从三星手机上的图库中选择图像,则图像将以横向方式加载;显然,我需要旋转图像。问题在于,当我尝试获取图像的方向时,为旋转值返回零(横向和纵向图像都有)。我目前无法弄清楚是否需要旋转这些图像。谢谢!

编辑:以下是我用来获取不起作用的方法的一些代码。我首先允许用户从图库中选择图像

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

           Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
           File photoFile = null;
           try {
               photoFile = createImageFile();
           } catch (IOException ex) {
           }
           // Continue only if the File was successfully created

           takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, savedUri);

           String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
           Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
           chooserIntent.putExtra
                   (
                           Intent.EXTRA_INITIAL_INTENTS,
                           new Intent[] { takePhotoIntent }
                   );

           startActivityForResult(chooserIntent, CAMERA_CAPTURE);

然后,在onActivityResult中,我们设置了Uri。之后,我尝试使用此代码获取方向:

       ExifInterface exif = new ExifInterface(picUri.getPath());

       String orientString = exif.getAttribute(ExifInterface.TAG_ORIENTATION);
       int orientation = orientString != null ? Integer.parseInt(orientString) : ExifInterface.ORIENTATION_NORMAL;
       if(orientation == 0){
           orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
       }
       if(orientation == 0) {
           String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION};
           Cursor cur = managedQuery(picUri, orientationColumn, null, null, null);
           if (cur != null && cur.moveToFirst()) {
               orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
           }
       }

0 个答案:

没有答案