0

Is it somehow possible to get a File object from an DocumentFile? With some small trick I can get the real path of the file, but the File class is not allowed to read/write there...

I need to access media files from USB OTG or secondary storage and I need following functions:

  • get exif data from images => I really need that for
    • getting the location of the image
    • getting the real creation date of the image
    • actually, for displaying purpose, I need all exif data
  • ability to rotate images (this would be possible by creating a temp image, delete the old one and rename the temp image)

Any idea on how to achieve that?

prom85
  • 16,896
  • 17
  • 122
  • 242

1 Answers1

2

Is it somehow possible to get a File object from an DocumentFile?

No.

With some small trick I can get the real path of the file

Not reliably. After all, the Storage Access Framework does not require there to be an actual file. Various DocumentProvider implementations work off of cloud services, where the data is not stored locally on the device until needed. Beyond that, whatever approach that you are using is dependent upon internal implementation that may vary by device, let alone Android OS version. And, to top it off, you still cannot necessarily access the data even if you derive a path, as you may not have filesystem access to that location (e.g., files held on internal storage by the DocumentProvider).

get exif data from images

Use the stream, along with EXIF code that can work with streams, such as this one or this one, found by searching the Internet for android exif stream.

ability to rotate images (this would be possible by creating a temp image, delete the old one and rename the temp image)

You don't have a choice to make a local copy, rotate the image, and then write the image back to the original DocumentFile using an OutputStream. Whether that "local copy" is only in RAM, or needs to be an actual file on disk, depends a bit on how you were planning on rotating it.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • for accessing LOCAL storages, that's really annoying... If the user allows my app to access the secondary storage / usb otg stick, there should be an easier way that that (I know, there isn't)... Thanks for the informations, I will try to do my tasks via the stream... – prom85 Jan 30 '16 at 21:09
  • PS: is there some way to get the image dimensions without "downloading" the complete file first? For local files I can use `BitmapFactory.Options` with `inJustDecodeBounds=true` to get image dimsensions very fast. Probably not, correct? – prom85 Jan 30 '16 at 21:15
  • @prom85: Well, `BitmapFactory` has `decodeStream()`. One hopes that it is optimized for the `inJustDecodeBounds=true` case to stop pulling data off the stream once the dimensions are known. That being said, I don't know if it *is* optimized that way (IIRC, the code is mostly native, not in Java). – CommonsWare Jan 30 '16 at 21:27
  • Thanks for the help, this should help me getting most of the thing working as I want it to work – prom85 Jan 30 '16 at 21:28