0

Android Saved Images into Gallery not Correctly Oriented The value obtained after int getOrientationFromExif() is always the same: 1... Don't know how to solve.... Please help me!

Can you help me to solve? The problem is inside

private PictureCallback mPicture = new PictureCallback()
{
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    public void onPictureTaken(byte[] data, Camera camera){

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);    

        try
        {
            FileOutputStream fos = new FileOutputStream(pictureFile);

            fos.write(data);
            fos.close(); 

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");

            Bitmap myBitmap = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            myBitmap = rotateImage(getOrientationFromExif(pictureFile), myBitmap); 
            myBitmap.compress(Bitmap.CompressFormat.JPEG, 70 /*ignored for PNG*/, bos);
            byte[] bitmapdata = bos.toByteArray();

            fos = new FileOutputStream(pictureFile);
            fos.write(bitmapdata);
            fos.close(); 

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
            Uri.parse("file://"+ mediaStorageDir)));
        }

        catch(FileNotFoundException e)
        {
            Log.d(TAG, "File not found: "+e.getMessage());
        }

        catch(IOException e)
        {
            Log.d(TAG, "Error accessing file: "+e.getMessage());
        }
    }
};

public Bitmap rotateImage(int angle, Bitmap bitmapSrc) 
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmapSrc, 0, 0, 
    bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}

public int getOrientationFromExif(File imagePath) 
{
    int orientation = -1;

    try 
    {   
        ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                ExifInterface.ORIENTATION_NORMAL);
        System.out.println("yuri"+exifOrientation);

        switch (exifOrientation) 
        {
            case ExifInterface.ORIENTATION_ROTATE_270:      
                orientation = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                orientation = 180;

                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                orientation = 90;

                break;

            case ExifInterface.ORIENTATION_NORMAL:
                orientation = 0;

                break;
            default:
                break;
        }
    } catch (IOException e) {
        Log.e(TAG, "Unable to get image exif orientation", e);
    }

    return orientation;
}

public static boolean isSdPresent() {   
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}


private static File getOutputMediaFile(int type){
    File mediaFile = null; 
    if(isSdPresent() == false)
    {
        Log.d(TAG, "There is no Sd card. Cannot use the camera");
    }

   else
    {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),".");

        if(!mediaStorageDir.exists())
        {
            if(!mediaStorageDir.mkdirs())
            {
                Log.d("WorldCupApp", "failed to create directory");
                return null;
            }
        }

         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());       
         if (type == MEDIA_TYPE_IMAGE)
         {        
             mediaFile = new File(mediaStorageDir.getPath() + "IMG_"+ timeStamp + ".JPEG");    
         } 
         else 
         {       
             return null;  
         }          
    }
    return mediaFile;
}       
Ravi
  • 34,851
  • 21
  • 122
  • 183
pb77
  • 47
  • 9
  • If this is about taking pictures you might want to check http://stackoverflow.com/questions/4645960/how-to-set-android-camera-orientation-properly – pentadecagon Jan 14 '14 at 11:41

2 Answers2

0
ExifInterface exif = new ExifInterface(imagePath.getAbsolutePath());
    int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
            ExifInterface.ORIENTATION_NORMAL);

Problem is here always is 1 the orientation that means that doesn't need to rotate :(

pb77
  • 47
  • 9
0

When you fix your activity to landscape mode for a custom camera implementation the EXIF data will always have ORIENTATION_NORMAL for TAG_ORIENTATION. This is the same side effect that has activity.getWindowManager().getDefaultDisplay().getRotation() always returning ROTATION_90.

I fixed this by detecting the device rotation with an OrientationEventListener and then rewriting the EXIF orientation tag in my onPictureTaken callback to the proper value for the rotation.

Not sure if this behavior is device dependent, this is the behavior on a Galaxy S2 running 4.1.2.

Mike
  • 1