How to find out if the image from the gallery was taken in portrait or landscape from camera









up vote
0
down vote

favorite












  • I have a image in gallery which was taken from camera

  • I am trying to find out if the image is portrait or landscape

I have the path obtained from below code as



/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg


Then i am using the ExifInterface to find the orientation:



 public static void getCameraPhotoOrientation(Activity activity, String imagePath) 



String path =getPath(activity,imagePath);


try
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation)
case ExifInterface.ORIENTATION_ROTATE_270:
Timber.d("Type-1");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Timber.d("Type-2");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Timber.d("Type-3");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Timber.d("Type-4");
break;


catch (IOException ex)
ex.printStackTrace();





I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




How to resolve this










share|improve this question





















  • Get image width and height, if height > width then it is portrait, if width > height then it is landscape, and height = width, square.
    – D. B.
    Nov 10 at 16:48














up vote
0
down vote

favorite












  • I have a image in gallery which was taken from camera

  • I am trying to find out if the image is portrait or landscape

I have the path obtained from below code as



/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg


Then i am using the ExifInterface to find the orientation:



 public static void getCameraPhotoOrientation(Activity activity, String imagePath) 



String path =getPath(activity,imagePath);


try
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation)
case ExifInterface.ORIENTATION_ROTATE_270:
Timber.d("Type-1");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Timber.d("Type-2");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Timber.d("Type-3");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Timber.d("Type-4");
break;


catch (IOException ex)
ex.printStackTrace();





I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




How to resolve this










share|improve this question





















  • Get image width and height, if height > width then it is portrait, if width > height then it is landscape, and height = width, square.
    – D. B.
    Nov 10 at 16:48












up vote
0
down vote

favorite









up vote
0
down vote

favorite











  • I have a image in gallery which was taken from camera

  • I am trying to find out if the image is portrait or landscape

I have the path obtained from below code as



/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg


Then i am using the ExifInterface to find the orientation:



 public static void getCameraPhotoOrientation(Activity activity, String imagePath) 



String path =getPath(activity,imagePath);


try
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation)
case ExifInterface.ORIENTATION_ROTATE_270:
Timber.d("Type-1");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Timber.d("Type-2");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Timber.d("Type-3");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Timber.d("Type-4");
break;


catch (IOException ex)
ex.printStackTrace();





I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




How to resolve this










share|improve this question













  • I have a image in gallery which was taken from camera

  • I am trying to find out if the image is portrait or landscape

I have the path obtained from below code as



/storage/emulated/0/DCIM/Camera/IMG_20181110_211757.jpg


Then i am using the ExifInterface to find the orientation:



 public static void getCameraPhotoOrientation(Activity activity, String imagePath) 



String path =getPath(activity,imagePath);


try
ExifInterface ei = new ExifInterface(path);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

switch (orientation)
case ExifInterface.ORIENTATION_ROTATE_270:
Timber.d("Type-1");
break;
case ExifInterface.ORIENTATION_ROTATE_180:
Timber.d("Type-2");
break;
case ExifInterface.ORIENTATION_ROTATE_90:
Timber.d("Type-3");
break;
case ExifInterface.ORIENTATION_UNDEFINED:
Timber.d("Type-4");
break;


catch (IOException ex)
ex.printStackTrace();





I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




How to resolve this







android exif






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 16:28









Devrath

22k37132186




22k37132186











  • Get image width and height, if height > width then it is portrait, if width > height then it is landscape, and height = width, square.
    – D. B.
    Nov 10 at 16:48
















  • Get image width and height, if height > width then it is portrait, if width > height then it is landscape, and height = width, square.
    – D. B.
    Nov 10 at 16:48















Get image width and height, if height > width then it is portrait, if width > height then it is landscape, and height = width, square.
– D. B.
Nov 10 at 16:48




Get image width and height, if height > width then it is portrait, if width > height then it is landscape, and height = width, square.
– D. B.
Nov 10 at 16:48












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted











I have a image in gallery which was taken from camera




There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.




I am trying to find out if the image is portrait or landscape




There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.




I have the path obtained from below code




That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri, use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri, then pass that InputStream to this ExifInterface constructor.




I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION.



If that tag does not exist, you could examine the width and height of the image and make a guess:



  • If the width is greater than the height, it might be landscape

  • If the height is greater than the width, it might be portrait

This will fail for square images, and this will fail for cropped images (e.g., the photo was taken portrait, but the user cropped it such that the width is now greater than the height).



The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.






share|improve this answer




















  • I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
    – shashank chandak
    Nov 10 at 17:15

















up vote
0
down vote













Try it by changing path accessing process like below



File imFile = new File(imagePath);
ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());


this will explain Android getAbsolutePath() not returning full path it






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240985%2fhow-to-find-out-if-the-image-from-the-gallery-was-taken-in-portrait-or-landscape%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted











    I have a image in gallery which was taken from camera




    There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.




    I am trying to find out if the image is portrait or landscape




    There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.




    I have the path obtained from below code




    That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri, use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri, then pass that InputStream to this ExifInterface constructor.




    I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




    Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION.



    If that tag does not exist, you could examine the width and height of the image and make a guess:



    • If the width is greater than the height, it might be landscape

    • If the height is greater than the width, it might be portrait

    This will fail for square images, and this will fail for cropped images (e.g., the photo was taken portrait, but the user cropped it such that the width is now greater than the height).



    The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.






    share|improve this answer




















    • I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
      – shashank chandak
      Nov 10 at 17:15














    up vote
    2
    down vote



    accepted











    I have a image in gallery which was taken from camera




    There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.




    I am trying to find out if the image is portrait or landscape




    There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.




    I have the path obtained from below code




    That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri, use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri, then pass that InputStream to this ExifInterface constructor.




    I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




    Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION.



    If that tag does not exist, you could examine the width and height of the image and make a guess:



    • If the width is greater than the height, it might be landscape

    • If the height is greater than the width, it might be portrait

    This will fail for square images, and this will fail for cropped images (e.g., the photo was taken portrait, but the user cropped it such that the width is now greater than the height).



    The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.






    share|improve this answer




















    • I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
      – shashank chandak
      Nov 10 at 17:15












    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted







    I have a image in gallery which was taken from camera




    There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.




    I am trying to find out if the image is portrait or landscape




    There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.




    I have the path obtained from below code




    That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri, use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri, then pass that InputStream to this ExifInterface constructor.




    I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




    Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION.



    If that tag does not exist, you could examine the width and height of the image and make a guess:



    • If the width is greater than the height, it might be landscape

    • If the height is greater than the width, it might be portrait

    This will fail for square images, and this will fail for cropped images (e.g., the photo was taken portrait, but the user cropped it such that the width is now greater than the height).



    The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.






    share|improve this answer













    I have a image in gallery which was taken from camera




    There is no way for a developer to guarantee that any particular image came from a camera app, unless the developer is the author of that camera app.




    I am trying to find out if the image is portrait or landscape




    There is no requirement for an image to be either portrait or landscape. For example, the camera app might crop its photos to be circular or square.




    I have the path obtained from below code




    That code will fail for lots of Uri values on lots of devices. If you want to use ExifInterface for a Uri, use getContentResolver().openInputStream() to get an InputStream on the content identified by the Uri, then pass that InputStream to this ExifInterface constructor.




    I always get orientation as ExifInterface.ORIENTATION_UNDEFINED




    Images do not have to have EXIF headers, let alone ExifInterface.TAG_ORIENTATION.



    If that tag does not exist, you could examine the width and height of the image and make a guess:



    • If the width is greater than the height, it might be landscape

    • If the height is greater than the width, it might be portrait

    This will fail for square images, and this will fail for cropped images (e.g., the photo was taken portrait, but the user cropped it such that the width is now greater than the height).



    The best solution is to change your app to not care whether the image is portrait or landscape. The next-best solution is to ask the user how they want to handle the image, where perhaps you use the above algorithm to set the default option.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 16:45









    CommonsWare

    760k13718511904




    760k13718511904











    • I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
      – shashank chandak
      Nov 10 at 17:15
















    • I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
      – shashank chandak
      Nov 10 at 17:15















    I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
    – shashank chandak
    Nov 10 at 17:15




    I have been reading your answers from a very long time now and wanted to really thank you , you write very detailed and brilliant answers . Could you please help me with my question here
    – shashank chandak
    Nov 10 at 17:15












    up vote
    0
    down vote













    Try it by changing path accessing process like below



    File imFile = new File(imagePath);
    ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());


    this will explain Android getAbsolutePath() not returning full path it






    share|improve this answer
























      up vote
      0
      down vote













      Try it by changing path accessing process like below



      File imFile = new File(imagePath);
      ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());


      this will explain Android getAbsolutePath() not returning full path it






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Try it by changing path accessing process like below



        File imFile = new File(imagePath);
        ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());


        this will explain Android getAbsolutePath() not returning full path it






        share|improve this answer












        Try it by changing path accessing process like below



        File imFile = new File(imagePath);
        ExifInterface ei = new ExifInterface(imFile.getAbsolutePath());


        this will explain Android getAbsolutePath() not returning full path it







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 16:41









        Lucefer

        1,0481511




        1,0481511



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240985%2fhow-to-find-out-if-the-image-from-the-gallery-was-taken-in-portrait-or-landscape%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20