android – File sharing of encrypted content-ThrowExceptions

Exception or error:

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?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *