Search code examples
androiddatabaserealm

Using Realm as a secure file storage and file reader


I haven't been able to find a clear answer to this question, so here it is;

I want to use Realm for Android to store files in an encrypted way, then be able to open/view those files without needing to actually save them in the phone. Those would be sensitive data file so It would be perfect if it stay in the local database of my app.

Is Realm well suited for my case or if not, what else would be for Android?


Solution

  • Realm suits or not. You may need to reconsider.

    If you just want to encrypt a database, it is easy. Just give it an encryption key when you init a Realm instance.

    However, there are some side effects.

    1. Save big files to Realm will slow it down.
    2. The danger of data corruption.
      If you database file is corrupted, all data is lost. So you have to backup. That means saving files into Realm will cause your app takes at least twice the size of saving the files in filesystem directly.
    3. The encryption key.
      You must design an encryption key that basing on each device other than just using one same key in your app. Or someone just needs to copy the database file from another's phone to his own and your app will decrypt the database for him.