Skip to content

Commit f7ddf4f

Browse files
committed
Fixed issue #105
1 parent 39c5994 commit f7ddf4f

File tree

5 files changed

+22
-9
lines changed

5 files changed

+22
-9
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ You can read file with helper functions in `DocumentFileCompat` and `MediaStoreC
8282

8383
#### Example
8484
```kotlin
85-
val fileFromExternalStorage = DocumentFileCompat.fromSimplePath(context, basePath = "Downloads/MyMovie.mp4")
85+
val fileFromExternalStorage = DocumentFileCompat.fromSimplePath(context, basePath = "Download/MyMovie.mp4")
8686

87-
val fileFromSdCard = DocumentFileCompat.fromSimplePath(context, storageId = "9016-4EF8", basePath = "Downloads/MyMovie.mp4")
87+
val fileFromSdCard = DocumentFileCompat.fromSimplePath(context, storageId = "9016-4EF8", basePath = "Download/MyMovie.mp4")
8888
```
8989

9090
### `MediaStoreCompat`
@@ -259,7 +259,8 @@ whenever a conflict is found via `onConflict()`. Here're screenshots of the samp
259259
![Alt text](art/parent-folder-conflict.png?raw=true "Parent Folder Conflict")
260260
![Alt text](art/folder-content-conflict.png?raw=true "Folder Content Conflict")
261261

262-
Read `MainActivity` from the sample code if you want to mimic above dialogs.
262+
Read [`MainActivity`](https://github.com/anggrayudi/SimpleStorage/blob/master/sample/src/main/java/com/anggrayudi/storage/sample/activity/MainActivity.kt)
263+
from the sample code if you want to mimic above dialogs.
263264

264265
## FAQ
265266

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ kotlin.code.style=official
2222
# For publishing:
2323
GROUP=com.anggrayudi
2424
POM_ARTIFACT_ID=storage
25-
VERSION_NAME=1.5.1-SNAPSHOT
25+
VERSION_NAME=1.5.2-SNAPSHOT
2626
SONATYPE_HOST=DEFAULT
2727
SONATYPE_AUTOMATIC_RELEASE=true
2828
RELEASE_SIGNING_ENABLED=false

storage/src/main/java/com/anggrayudi/storage/SimpleStorage.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,12 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {
529529

530530
private fun checkRequestCode() {
531531
val set = setOf(requestCodeFilePicker, requestCodeFolderPicker, requestCodeStorageAccess, requestCodeCreateFile)
532-
if (set.size < 4)
532+
if (set.size < 4) {
533533
throw IllegalArgumentException(
534534
"Request codes must be unique. File picker=$requestCodeFilePicker, Folder picker=$requestCodeFolderPicker, " +
535535
"Storage access=$requestCodeStorageAccess, Create file=$requestCodeCreateFile"
536536
)
537+
}
537538
}
538539

539540
private fun saveUriPermission(root: Uri) = try {

storage/src/main/java/com/anggrayudi/storage/file/DocumentFileCompat.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,10 @@ object DocumentFileCompat {
493493
if (currentDirectory.isRawFile) {
494494
return tryCreateWithRawFile()
495495
}
496+
val rootBasePath = currentDirectory.getAbsolutePath(context)
497+
val nextBasePath = fullPath.replaceFirst(rootBasePath, "").trimFileSeparator()
496498
val resolver = context.contentResolver
497-
getDirectorySequence(getBasePath(context, fullPath)).forEach {
499+
getDirectorySequence(nextBasePath).forEach {
498500
try {
499501
val directory = currentDirectory.quickFindTreeFile(context, resolver, it)
500502
currentDirectory = when {

storage/src/main/java/com/anggrayudi/storage/media/MediaStoreCompat.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ object MediaStoreCompat {
116116
}
117117
if (mode == CreateMode.REPLACE) {
118118
existingMedia.delete()
119-
return MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
119+
return tryInsertMediaFile(context, mediaType, contentValues)
120120
}
121121

122122
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
@@ -138,9 +138,9 @@ object MediaStoreCompat {
138138
?.let { return it }
139139
}
140140

141-
MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
141+
tryInsertMediaFile(context, mediaType, contentValues)
142142
}
143-
else -> MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
143+
else -> tryInsertMediaFile(context, mediaType, contentValues)
144144
}
145145
} else {
146146
@Suppress("DEPRECATION")
@@ -165,6 +165,15 @@ object MediaStoreCompat {
165165
}
166166
}
167167

168+
private fun tryInsertMediaFile(context: Context, mediaType: MediaType, contentValues: ContentValues): MediaFile? {
169+
return try {
170+
MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
171+
} catch (e: Exception) {
172+
e.printStackTrace()
173+
null
174+
}
175+
}
176+
168177
/**
169178
* This action only deletes your app's created files.
170179
* @see MediaFile.owner

0 commit comments

Comments
 (0)