4

I need to read some Exif properties of an Image (taken with the Camera or picked from the Gallery).

Here is how I launch the Camera:

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    File file = new File(myObject.getLocalUrl());
    Uri fileURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileURI);
    startActivityForResult(intent, CAPTURE_IMAGE_REQUEST_CODE);

Here is how I launch the Gallery Picker:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(intent, getString(R.string.image_picker_select_image)), SELECT_IMAGE_FROM_GALLERY_REQUEST_CODE);

The problem is that for example, in the first case (Camera), after the Image is taken and saved in the External Storage, the Exif information get lost.

The same for the Images picked from the Gallery. That's how I get the Image in the onActivityResult method:

Uri uri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

And finally here is how I read the Exif data:

ExifInterface exifInterface = null;
    try {
        exifInterface = new ExifInterface(myObject.getLocalUrl());
    } catch (IOException e) {
        e.printStackTrace();
    }
    String exifOrientation = exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION);
    String exifMake = exifInterface.getAttribute(ExifInterface.TAG_MAKE);
    String exifModel = exifInterface.getAttribute(ExifInterface.TAG_MODEL);

I've tried the next:

  1. Take a picture using my device Camera (without using my App).
  2. Read the Exif data using ExifInterface.
  3. It works like a charm.

So I guess the problem is that when the Image is saved (after it's taken with the Camera) or the Image is picked from the Gallery, the Exif data gets lost.

I've read at least 20-30 post here in Stackoverflow but basically the problem that everyone has is that the orientation exif information is lost, so the solution is writing the right orientation in the Exif data.

That solution is not fine for me since I don't want to overwrite the Exif data, I just want to read the original one.

Any ideas/hint? Thanks.

Ale
  • 2,282
  • 5
  • 38
  • 67
  • `new ExifInterface(myObject.getLocalUrl());`. What is myObject? What does it have to do with onActivityResult()? Better show complete code. – greenapps Feb 09 '17 at 11:26
  • `That's how I get the Image in the onActivityResult method:`. Irrelevant. Does not matter. You should show us how you get the exif from the .jpg file. – greenapps Feb 09 '17 at 11:29
  • `the problem that everyone has is that the orientation exif information is lost, so the solution is writing the right orientation in the Exif data.`. This all makes no sense to me. JPG files do not loose exif information. – greenapps Feb 09 '17 at 11:32
  • `in the first case (Camera), after the Image is taken and saved in the External Storage, the Exif information get lost.`. ? The Camera app will save the image to a .jpg file. With an exif header. Thats all. You are not saving something again is it? Please show complete onActivityResult code. – greenapps Feb 09 '17 at 11:35
  • `2. Read the Exif data using ExifInterface.`. Well what are you doing different then? Picking from gallery or how? – greenapps Feb 09 '17 at 11:38
  • `need to read some Exif properties of an Image`. Very vague. You can only read exif info from a .jpg file. – greenapps Feb 09 '17 at 11:40
  • `new ExifInterface(myObject.getLocalUrl());` --> `myObject.getLocalUrl()` is the url where I want to save the Image file, something like `/storage/emulated/0/Pictures/test.jpg`. It has nothing to do with onActivityResult() as I said, in the case of the camera Intent – Ale Feb 09 '17 at 11:42
  • `Irrelevant. Does not matter. You should show us how you get the exif from the .jpg file` Actually I think it does matters, because maybe that`s the reason because I'm not getting the correct exif data, because the Uri is wrong, or the way I'm retrieving the information is wrong – Ale Feb 09 '17 at 11:44
  • `The Camera app will save the image to a .jpg file. With an exif header. Thats all. You are not saving something again is it? Please show complete onActivityResult code.` onActivityResult does not have anything to do here since 'intent.putExtra(MediaStore.EXTRA_OUTPUT, fileURI);' will save the image in a file – Ale Feb 09 '17 at 11:45
  • It is unclear what you want to save. And if it has nothing to do with onActivity result then what can it have to do with a Camera intent? And you just did not tell what myObject is and what it has to do with a camera intent. – greenapps Feb 09 '17 at 11:46
  • `Read the Exif data using ExifInterface`. I really appreciate your help but maybe you could try to understand the post before questioning every line I wrote. As I said, there are 2 ways of selecting an Image, from the gallery or from the camera – Ale Feb 09 '17 at 11:48
  • Alright enough, please if you are not going to help, just stop telling me how bad is my question, we all know you are so great but it's not my case, that's why I wrote a post here :) – Ale Feb 09 '17 at 11:51
  • `intent.putExtra(MediaStore.EXTRA_OUTPUT, fileURI);' will save the image in a file`. Well.. Not that intent but the camera app will produce a file using that file uri. And after that you can use fileUri to open the jpg file. But i dont see you doing that. If you are not trying to open the file in onActivity result then you dont have to show us any code from onActivityResult either. Nor any code that retrieves a bitmap. WHAT IS myObject? – greenapps Feb 09 '17 at 11:52
  • `catch (IOException e) { e.printStackTrace(); }`. Do you have a catch there? Add a Toast() in the catch block to tell the user e.getMessage(). And return;! You are now continuing with the code as if nothing has happened. – greenapps Feb 09 '17 at 12:01
  • `new ExifInterface(myObject.getLocalUrl());`. After rereading your post it looks as if that code is correct. – greenapps Feb 09 '17 at 12:05
  • `finally here is how I read the Exif data:`. You reported that exif data gets lost. But you did not tell us which values you got in the three strings. – greenapps Feb 09 '17 at 12:07
  • `same for the Images picked from the Gallery`. It is unclear which code or path you use to determine the exif of the choosen .jpg file. Your whole story is pretty inconsistent. – greenapps Feb 09 '17 at 12:12
  • Honestly...do I really have to close the post or can you just stop, PLEASE? come on just be happy for one day buddy, life is so wonderful don't be so mad. Yes you are the best, if you had written the question it would be way better, the best question ever. Now, can you stop, please? – Ale Feb 09 '17 at 12:16
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135267/discussion-between-ale-and-greenapps). – Ale Feb 09 '17 at 12:17
  • You don't have to close the post. You just should give the info you did not tell and is needed to solve your problem. You come here for help and i gave you a lot but you fail to see that it is real help. The description of your problem is really inconsistent but you seem reluctant to better it. – greenapps Feb 09 '17 at 12:22
  • Did u find the solution to the Problem, i'm facing the same issue. A little background info: Before calling Intent to open Camera App to capture image for us, I create file in ExternalFilesDirectory and then get its Uri either by `Uri photoURI = FileProvider.getUriForFile(mActivity, authorities, photoFile);` or `Uri photoURI = Uri.fromFile(photoFile);` depending on the **android.os.Build.VERSION.SDK_INT** of device. and then send that Uri with the intent for Camera image capture And in onActivityResult method i try to get Exif info from that Uri and from imgPath as well but still get no Exif – Faizy_Flash Feb 26 '18 at 13:07
  • Continued: I mean i get Exif data like this for most devices fine, But on Samsung Note 2, for the orientation data in Exif it keeps giving me default value of 1. So am i doing something wrong or is there another way to get Exif data (especially orientation info) for the Image. @CommonsWare if u can see this ur help will be much appreciated as i still cant find a solution to this even after reading many threads on this topic. – Faizy_Flash Feb 26 '18 at 13:12

1 Answers1

0

Try with below method: pass Uri into this method

 public static int getOrientation(Context context, Uri photoUri) {
           Cursor cursor = context.getContentResolver().query(photoUri,
           new String[] { MediaStore.Images.ImageColumns.ORIENTATION },null, null, null);
           try {
                if (cursor.moveToFirst()) {
                    return cursor.getInt(0);
                } else {
                    return -1;
                }
            } finally {
                cursor.close();
            }
        }

There are many ImageColumns are available regrading image information.

Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • I have tried using the `ExifInterface` method and this method and in both cases I only get `0` as the orientation for every single picture in the two phones I have tested (Moto G4 an Samsung Galaxy J5) – Nicolás Carrasco-Stevenson May 12 '17 at 21:28