Skip to content

Commit b09da38

Browse files
authored
Fixes android external dir absolute path being null (#343)
1 parent 7706606 commit b09da38

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

android/src/main/java/com/op/sqlite/OPSQLiteModule.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ import java.io.InputStream
1212
import java.io.OutputStream
1313
import com.facebook.react.util.RNLog;
1414

15-
//@ReactModule(name = OPSQLiteModule.NAME)
1615
internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextBaseJavaModule(context) {
1716
override fun getName(): String {
1817
return NAME
1918
}
2019

21-
fun getTypedExportedConstants(): MutableMap<String, Any> {
22-
val constants: MutableMap<String, Any> = HashMap()
20+
fun getTypedExportedConstants(): MutableMap<String, Any?> {
21+
val constants: MutableMap<String, Any?> = HashMap()
2322
val context = reactApplicationContext
2423
val dbPath =
2524
context.getDatabasePath("defaultDatabase")
@@ -28,14 +27,14 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextB
2827
constants["ANDROID_DATABASE_PATH"] = dbPath
2928
val filesPath = context.filesDir.absolutePath
3029
constants["ANDROID_FILES_PATH"] = filesPath
31-
val externalFilesDir = context.getExternalFilesDir(null)!!.absolutePath
32-
constants["ANDROID_EXTERNAL_FILES_PATH"] = externalFilesDir
33-
constants["IOS_DOCUMENT_PATH"] = ""
34-
constants["IOS_LIBRARY_PATH"] = ""
30+
val externalFilesDir = context.getExternalFilesDir(null)
31+
constants["ANDROID_EXTERNAL_FILES_PATH"] = externalFilesDir?.absolutePath
32+
constants["IOS_DOCUMENT_PATH"] = null
33+
constants["IOS_LIBRARY_PATH"] = null
3534
return constants
3635
}
3736

38-
override fun getConstants(): MutableMap<String, Any>? {
37+
override fun getConstants(): MutableMap<String, Any?> {
3938
return getTypedExportedConstants()
4039
}
4140

@@ -53,7 +52,6 @@ internal class OPSQLiteModule(context: ReactApplicationContext?) : ReactContextB
5352
@ReactMethod(isBlockingSynchronousMethod = true)
5453
fun getDylibPath(bundleId: String, name: String): Boolean {
5554
throw Exception("Do not call getDylibPath on Android")
56-
return true
5755
}
5856

5957
@ReactMethod

docs/docs/configuration.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,15 @@ const db = open({
4545
});
4646
```
4747

48-
If you want to access the SD card app's directory:
48+
If you want to access the SD card app's directory. However, `ANDROID_EXTERNAL_FILES_PATH` **can be null**, sometimes there are OS/Device level errors on which the OS does not detect the directory correctly. You should always have a fallback.
4949

5050
```tsx
5151
const db = open({
5252
name: 'myDB',
5353
location:
54-
Platform.OS === 'ios' ? IOS_LIBRARY_PATH : ANDROID_EXTERNAL_FILES_PATH,
54+
Platform.OS === 'ios'
55+
? IOS_LIBRARY_PATH
56+
: ANDROID_EXTERNAL_FILES_PATH ?? ANDROID_DATABASE_PATH,
5557
});
5658
```
5759

@@ -63,7 +65,7 @@ const db = open({
6365
location:
6466
Platform.OS === 'ios'
6567
? IOS_LIBRARY_PATH
66-
: `${ANDROID_EXTERNAL_FILES_PATH}/dbs/`,
68+
: `${ANDROID_EXTERNAL_FILES_PATH}/databases/`,
6769
});
6870
```
6971

0 commit comments

Comments
 (0)