2

My app manages different types of files (word documents, images, text, ...) and has the ability to share, open and receive those. This is implemented with a custom ContentProvider, using its openFile(Uri uri, String mode) method. Recently I have implemented storing the files on external storage while encrypting them with Androids EncryptedFile. I ended up implementing a mix of the solutions from this question:

  • If the file mode contains "w", create a pipe via ParcelFileDescriptor.createPipe(...), connect one end to a writer thread and return the write side.
  • Otherwise create an unencrypted copy of the file on internal storage and return that via ParcelFileDescriptor.open(...).

This works, but the approach with the temporary file has several problems:

  • If I remove the file when ParcelFileDescriptor.OnCloseListeners onClose(...) is called, I might end up creating the temporary file multiple times, as openFile(...) might get called multiple times. This can be really slow for bigger files.
  • If I implement removing temporary files somewhen later so that a temporary file might get reused when openFile(...) is called, I need to implent some kind of check, as the content of the original file might have changed in the meantime. That is essentially caching, which is one of the harder problems in computer science.

Is there a better way to handle encrypted files? Or is this currently the best one?

Kai Hatje
  • 431
  • 6
  • 17
  • "has the ability to share, open and receive those" -- under what circumstances are you using this `ContentProvider`? If it is purely for within your own app, I'd nuke the `ContentProvider` entirely. "Is there a better way to handle encrypted files?" -- IMHO, your problem isn't strictly with encryption. You would have the same problem if these were videos that you were dynamically transcoding, for example. There is nothing really in the Android SDK that amounts to "`FileProvider` but with custom data conversion" (which is really what it appears that you need). – CommonsWare Mar 05 '20 at 19:33
  • Examples are opening an internal file with the word app or taking a picture with the camera app that should be stored in an internal file. This is achieved with an intent pointing to a content Uri which uses the `ContentProvider`. – Kai Hatje Mar 06 '20 at 09:49
  • That's reasonable. However, on the whole, you are going to be pretty much on your own for the sorts of things that you highlight in your bullets. – CommonsWare Mar 06 '20 at 11:53

0 Answers0