Get Exif data from ACTION_SEND intent (TAG_GPS_LAT

2019-08-10 10:31发布

问题:

It seems like this question is everywhere on SO but nothing is working. I am sending an intent from the Android camera to my app (sharing a image taken with my Android camera to my app).

in my framgent:

// ...
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = null;

    Intent intent = getActivity().getIntent();
    String action = intent.getAction();
    String type = intent.getType();
    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
        // ...
        } else if (type.startsWith("image/")) {
            Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (imageUri != null) {
                Log.v(null, imageUri.getPath());
                try {
                    ExifInterface exif = new ExifInterface(imageUri.getPath());
                    String str = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
                    Log.v(null, "lat "+str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                // ...
            }
        }
    } else {
        view = inflater.inflate(R.layout.fragment_main, container, false);
        initComponents(view);
        initListeners();
    }
    return view;
}
// ...

Log.v(null, imageUri.getPath()); prints out /external/images/media/5670

The error appears: E/JHEAD﹕ can't open '/external/images/media/5670' and then Log.v(null, "lat "+str); prints out lat null?

回答1:

imageUri.getPath() does not return a filesystem path, because a Uri is not a file. The weak implementation of ExifInterface that is supplied in the Android SDK does not support reading in from an InputStream, which is what you would need to consume that Uri reliably, using a ContentResolver.

You will probably need to switch to using another EXIF library. I am using Google's AOSP one from the Mms app in my CWAC-Camera library.