0

I want to store pdf, word, excel (...) files into the sqlite Database as BLOB data and then, retrieve it to be open using a third party application.

Because those files are confidential, I don't want them to be stored in the phone while reading them. (If the phone is stolen, I don't want stranger to have access to the file)

What are my options?

William Lyon
  • 8,371
  • 1
  • 17
  • 22
Jaythaking
  • 2,200
  • 4
  • 25
  • 67

1 Answers1

1

I don't want them to be stored in the phone while reading them

Technically, you do not have control over that. The third-party app is welcome to do whatever it wants with the data, which includes saving it to a file, sharing it over the Internet, etc.

You are welcome to write a ContentProvider that streams your documents in response to openFile(). In your case, since the content is not a file, you would need to use a ParcelFileDescriptor pipe, so you can write your data to an OutputStream. This sample project demonstrates the basic technique, though I am serving a PDF from assets.

I would not be surprised if you run into problems with larger documents, though. The client app may want to seek forwards and backwards in the stream, and pipes do not result in seekable streams.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • In this example, the file is stored in the Asset folder... Also, I though the ParcelFileDescriptor needed an URI to be created? The URI is generated using a file that is already stored to a specific location, which I want to avoid – Jaythaking Mar 01 '16 at 21:34
  • @Jaythaking: "the file is stored in the Asset folder" -- yes. I mentioned that in my answer. "I though the ParcelFileDescriptor needed an URI to be created?" -- the client has a `Uri` and uses that with a `ContentResolver` and `openInputStream()` to read in the content. "The URI is generated using a file that is already stored to a specific location" -- I happened to use that, for consistency with other book examples. You can have your `Uri` path be whatever you want, so long as you know how to map that to your desired database entries. – CommonsWare Mar 01 '16 at 21:38
  • Do you have book or any website to suggest me where they use database as input for ContentResolver? I'm a little lost here as there are not many example of this out there... – Jaythaking Mar 01 '16 at 21:47
  • @Jaythaking: I am not aware of one for `openFile()`. There are lots of database `ContentProvider` examples, but they are mostly for the database APIs (`query()`, `insert()`, `update()`, `delete()`). Few people go the put-large-docs-in-a-BLOB-column route. You might consider using IOCipher instead of rolling your own encrypted-doc-in-a-database implementation. – CommonsWare Mar 01 '16 at 21:50
  • Also if the file isn't seekable, thats a big issue... Is there any other solution? If I have to store the file in the phone, I want at least to store it encrypted... What are the best practice in 2016 for that? All the other answer on the subject are outdated... Thx a lot for your help btw :) – Jaythaking Mar 01 '16 at 21:54
  • 1
    @Jaythaking: "Is there any other solution?" -- none that I am aware of, other than using libraries to render these files yourself. The primary limitation is in making content available to arbitrary third-party apps. You have to use the protocols that those apps are expecting. "What are the best practice in 2016 for that?" -- well, most developers don't worry about it, relying on device encryption and the intrinsic security of internal storage. If I were in your shoes, I would use IOCipher, as previously suggested. – CommonsWare Mar 01 '16 at 21:59