Skip to content

Commit

Permalink
Fixed issue #105
Browse files Browse the repository at this point in the history
  • Loading branch information
anggrayudi committed Dec 7, 2022
1 parent 39c5994 commit f7ddf4f
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ You can read file with helper functions in `DocumentFileCompat` and `MediaStoreC

#### Example
```kotlin
val fileFromExternalStorage = DocumentFileCompat.fromSimplePath(context, basePath = "Downloads/MyMovie.mp4")
val fileFromExternalStorage = DocumentFileCompat.fromSimplePath(context, basePath = "Download/MyMovie.mp4")

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

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

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

## FAQ

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kotlin.code.style=official
# For publishing:
GROUP=com.anggrayudi
POM_ARTIFACT_ID=storage
VERSION_NAME=1.5.1-SNAPSHOT
VERSION_NAME=1.5.2-SNAPSHOT
SONATYPE_HOST=DEFAULT
SONATYPE_AUTOMATIC_RELEASE=true
RELEASE_SIGNING_ENABLED=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,12 @@ class SimpleStorage private constructor(private val wrapper: ComponentWrapper) {

private fun checkRequestCode() {
val set = setOf(requestCodeFilePicker, requestCodeFolderPicker, requestCodeStorageAccess, requestCodeCreateFile)
if (set.size < 4)
if (set.size < 4) {
throw IllegalArgumentException(
"Request codes must be unique. File picker=$requestCodeFilePicker, Folder picker=$requestCodeFolderPicker, " +
"Storage access=$requestCodeStorageAccess, Create file=$requestCodeCreateFile"
)
}
}

private fun saveUriPermission(root: Uri) = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,10 @@ object DocumentFileCompat {
if (currentDirectory.isRawFile) {
return tryCreateWithRawFile()
}
val rootBasePath = currentDirectory.getAbsolutePath(context)
val nextBasePath = fullPath.replaceFirst(rootBasePath, "").trimFileSeparator()
val resolver = context.contentResolver
getDirectorySequence(getBasePath(context, fullPath)).forEach {
getDirectorySequence(nextBasePath).forEach {
try {
val directory = currentDirectory.quickFindTreeFile(context, resolver, it)
currentDirectory = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ object MediaStoreCompat {
}
if (mode == CreateMode.REPLACE) {
existingMedia.delete()
return MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
return tryInsertMediaFile(context, mediaType, contentValues)
}

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

MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
tryInsertMediaFile(context, mediaType, contentValues)
}
else -> MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
else -> tryInsertMediaFile(context, mediaType, contentValues)
}
} else {
@Suppress("DEPRECATION")
Expand All @@ -165,6 +165,15 @@ object MediaStoreCompat {
}
}

private fun tryInsertMediaFile(context: Context, mediaType: MediaType, contentValues: ContentValues): MediaFile? {
return try {
MediaFile(context, context.contentResolver.insert(mediaType.writeUri!!, contentValues) ?: return null)
} catch (e: Exception) {
e.printStackTrace()
null
}
}

/**
* This action only deletes your app's created files.
* @see MediaFile.owner
Expand Down

0 comments on commit f7ddf4f

Please sign in to comment.