How to set RealmDefaultConfiguration with encryption key - android

I am using realm database in my app, and currently in the Application class I am initialising realm with default configuration and everywhere in the app I am using Realm.getDefaultConfiguration() to query/save data.
Now I wanted to encrypt the database and I did as following
RealmConfiguration config = new RealmConfiguration.Builder()
.encryptionKey(getKeyFunction())
.migration(new MyMigration())
.build();
Realm.setDefaultConfiguration(config);`
But when I try to access Realm.getDefaultConfiguration() I get Invalid format of Realm File error.
What am I doing wrong ?

Here is my working code. I have tested this in my sample project
// Generate a key
// IMPORTANT! This is a silly way to generate a key. It is also never stored.
// For proper key handling please consult:
// * https://developer.android.com/training/articles/keystore.html
// * http://nelenkov.blogspot.dk/2012/05/storing-application-secrets-in-androids.html
Realm.init(this);
byte[] key = new byte[64];
new SecureRandom().nextBytes(key);
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
.encryptionKey(key)
.build();
// Start with a clean slate every time
Realm.deleteRealm(realmConfiguration);
Realm.setDefaultConfiguration(realmConfiguration);
// Open the Realm with encryption enabled
realm = Realm.getDefaultInstance();
//realm = Realm.getInstance(realmConfiguration);
// Everything continues to work as normal except for that the file is encrypted on disk
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
Person person = realm.createObject(Person.class);
person.setName("Happy Person");
person.setAge(14);
}
});
Person person = realm.where(Person.class).findFirst();
Log.i(TAG, String.format("Person name: %s", person.getName()));

Related

Storing unicode characters with Realm

I want to read a JSON string from a text file and store it's objects to a Realm file.
Text file created with Delphi and encoded in UTF-8. I'm reading String from text-file with a Scanner class and then extract from it some JSONObjects and JSONArrays. there is no problem in JSON Objects and Arrays with unicode characters. I put them in the realm file with createAllFromJson method of Realm object :
RealmConfiguration RealmConfig = new RealmConfiguration.Builder()
.name("info.realm")
.deleteRealmIfMigrationNeeded()
.build();
Realm realm = Realm.getInstance(RealmConfig);
try{
realm.executeTransaction(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.createAllFromJson(AccountRecordObject.class, accounts);
realm.createAllFromJson(SanadRecordObject.class, sanads);
realm.createObjectFromJson(ConfigRecordObject.class, config);
}
});
}finally {
realm.close();
}
and reading from Realm object :
RealmConfiguration RealmConfig = new RealmConfiguration.Builder()
.name("info.realm")
.deleteRealmIfMigrationNeeded()
.build();
Realm realm = Realm.getInstance(RealmConfig);
try{
TextView txt = parentView.findViewById(R.id.fileSampleTxt);
String S = realm.where(AccountRecordObject.class).equalTo("AccNo", 300015).findFirst().getAccName();
txt.setText(S);
}finally {
realm.close();
}
The problem is when I want to get a result from Realm file, unicode characters are shown as '?' :
Edit :
There is no problem in unicode characters when I use createObject method instead of createAllFromJson and passing a value from JSONArray :
AccountRecordObject obj = realm.createObject(AccountRecordObject.class);
obj.setAccNo(10000001);
try {
obj.setAccName(accounts.getJSONObject(4).getString("AccName"));
}catch (JSONException E){
E.printStackTrace();
}
It seems there is a problem in createAllFromJson and createObjectFromJson methods with unicode characters
Realm stores unicode just fine, so most likely it is the font used in Android Studio that does not support the glyphs actually in the file.

Get Data From Realm File in SdCard or Another Path

I have a db.realm file in AssetFolder and In First Start My App I Copy This file To This Address :
/data/data/" + getPackageName()+"/files/
I want to Get Data From This File In android How Can I do It ?
Exactly How Can I Config realmfile from Path?
I use This Code :
RealmConfiguration configC = new RealmConfiguration.Builder("/data/data/" + getPackageName()+"/files/")
.deleteRealmIfMigrationNeeded()
.modules(new WordRealm())
.build();
Then I got This Error:
note 1 : I donot want to read realmfile from asset like this code :
RealmConfiguration config = new RealmConfiguration.Builder()
.assetFile("path/to/file/in/assets") // e.g "default.realm" or "lib/data.realm"
.deleteRealmIfMigrationNeeded()
.build()
I want to get it from internal or external path.
note 2 : i use Realm Java 3.4.0
Thanks.
The default location for Realm files are in /data/data/<packageName>/files so doing this should work:
RealmConfiguration config = new RealmConfiguration.Builder()
.name("db.realm")
.deleteRealmIfMigrationNeeded()
.modules(new WordRealm())
.build();

Realm Android Decryption issue

When I try to access the realm db after installing the app, I'm getting the following error:
Caused by: java.lang.IllegalArgumentException: Wrong key used to decrypt Realm.
This is the method which return realm instance:
RealmConfiguration config = new RealmConfiguration.Builder(mContext)
.name(dbname)
.schemaVersion(0)
.migration(new DataBaseMigration())
.encryptionKey(key)
.build();
Realm realm = Realm.getInstance(config);
return realm;
I am getting the error in the following line.
Realm realm = Realm.getInstance(config);
Help me to solve this issue. I am using realm 0.85 version.

Realm with pre populated data into assets?

Normally I use Realm as:
RealmConfiguration config = new RealmConfiguration.Builder(applicationContext).deleteRealmIfMigrationNeeded().build();
How can I add to the assets folder of my project a database with data and read it?
Since Realm Java 0.91.0 there has been an assetFile(String) option on the RealmConfiguration that automatically will copy a file from assets and use that if needed (e.g. if the Realm is opened the first time or has been deleted for some reason):
RealmConfiguration config = new RealmConfiguration.Builder()
.assetFile("path/to/file/in/assets") // e.g "default.realm" or "lib/data.realm"
.deleteRealmIfMigrationNeeded()
.build()
The above will copy the file from assets the first time the Realm is opened or if it has been deleted due to migrations (remember to update the asset Realm in that case).
OLD ANSWER:
It is possible to bundle a Realm database in the assets folder, but then you just need to copy it from there when starting the app the first time.
We have an example of how to copy the files here: https://github.com/realm/realm-java/blob/master/examples/migrationExample/src/main/java/io/realm/examples/realmmigrationexample/MigrationExampleActivity.java#L101-Lundefined
copyBundledRealmFile(this.getResources().openRawResource(R.raw.default_realm), "default.realm");
private String copyBundledRealmFile(InputStream inputStream, String outFileName) {
try {
File file = new File(this.getFilesDir(), outFileName);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, bytesRead);
}
outputStream.close();
return file.getAbsolutePath();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Since Realm 0.89.0 RealmConfiguration.initialData(Realm.Transaction) can now be used to populate a Realm file before it is used for the first time.
RealmConfiguration conf = new RealmConfiguration.Builder(context)
.initialData(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
realm.beginTransaction();
realm.createObject(....)
realm.commitTransaction();
}
}).deleteRealmIfMigrationNeeded().name("mRealm.db").build();
Realm realm = Realm.getInstance(conf);
[EDIT] See Stan's answer below. Apparently Realm now supports this directly so you can ignore this answer (unless you're using older Realm versions).
We had a similar need, and also wanted support for a read-only realm database shared with an iOS version of the app.
We created a simple library and have open-sourced it. It includes the copy code given in #christian-melchior's answer, as well as some optional extra tracking for read-only realm database(s) bundled with the APK. Comments and PRs welcomed. See:
https://github.com/eggheadgames/android-realm-asset-helper
Realm has a special parameter in its RealmConfiguration.Builder called assetFile. You could use it like:
realmConfiguration = new RealmConfiguration.Builder()
.assetFile("dataBase/default.realm") // your app's packaged DB
...
.build();
just set yer assets DB path and file name and you are good to go without any android-realm-asset-helper lib or copy-file-from-assets code. In this example my app packaged DB-file lies in "assets/dataBase/default.realm".Note, version below 2 has a bit another way to call assetFile, you should pass context additionally:
realmConfiguration = new RealmConfiguration.Builder(context)
.assetFile(context, "dataBase/default.realm")
.build();
You can use assetFile() method. Please be aware that you can't use assetFile() with deleteIfMigrationNeeded().

Realm.deleteRealmFile() deprecated android?

Now that Realm.deleteRealmFile() is deprecated, what is the best way to remove the realm file and instantiate a new one in an android application?
I have tried setting a new configuration, though I am getting a bunch of Realm Migration errors? Why is this?
Per the API documentation, you should now use DeleteRealm(RealmConfiguration) instead, where RealmConfiguration specifies, among other things, the Realm file to be deleted. You can find the API documentation here: https://realm.io/docs/java/latest/api/
If you are stuck in MigrationNeededException loop, try this, it will delete the whole folder and rebuild the Realm.
private void rebuildDatabase() {
RealmConfiguration realmConfig = new RealmConfiguration.Builder(getApplicationContext())
.name(getString(R.string.realm_file_name))
.deleteRealmIfMigrationNeeded()
.build();
Realm.deleteRealm(realmConfig);
File dir = realmConfig.getRealmFolder();
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
Realm.setDefaultConfiguration(realmConfig);
Realm newRealm = Realm.getInstance(this);
newRealm.close();
}

Categories

Resources