@@ -36,6 +36,7 @@ import android.provider.MediaStore
3636import android.util.Log
3737import kotlinx.coroutines.Dispatchers
3838import kotlinx.coroutines.withContext
39+ import java.io.File
3940import java.time.Instant
4041import kotlin.coroutines.resume
4142import kotlin.coroutines.suspendCoroutine
@@ -150,9 +151,10 @@ class MediaStoreService(private val context: Context) {
150151 )?.use { cursor ->
151152 val columnIndexes = ColumnIndexes .fromCursor(cursor)
152153 while (cursor.moveToNext()) {
153- val image = getFromCursor(cursor, columnIndexes)
154- Log .d(TAG , " $image " )
155- images + = image
154+ getFromCursor(cursor, columnIndexes)?.let {
155+ Log .d(TAG , " $it " )
156+ images + = it
157+ }
156158 }
157159 }
158160 }
@@ -175,12 +177,13 @@ class MediaStoreService(private val context: Context) {
175177 return setOf<String >(Environment .getExternalStorageDirectory().canonicalPath).plus(volumes)
176178 }
177179
178- private fun guessRelativePath (absolutePath : String , displayName : String ): String {
180+ private fun guessRelativePath (absolutePath : String ): String {
179181 val rootPath = getVolumesPath().find { absolutePath.startsWith(it) }
180182 ? : throw Exception (" Unable to determine relative path for file $absolutePath " )
183+ val displayName = (File (absolutePath)).name
181184 // Compute relative path on sdk where relative path is not supported
182185 if (! absolutePath.endsWith(displayName)) {
183- throw Exception (" Unable to determine relative path for file $absolutePath - $displayName " )
186+ throw Exception (" Unable to determine relative path for file: $absolutePath display name: $displayName root path: $rootPath " )
184187 }
185188 // Relative path should looks like DCIM/ (no leading /, no filename)
186189 return absolutePath.substringAfter(rootPath).substringBefore(displayName).trimStart(' /' )
@@ -189,28 +192,32 @@ class MediaStoreService(private val context: Context) {
189192 private fun getFromCursor (
190193 cursor : Cursor ,
191194 columnIndexes : ColumnIndexes ,
192- ): MediaStoreFile {
195+ ): MediaStoreFile ? {
193196 val id = cursor.getLong(columnIndexes.idColumn)
194197 val dateModified = Instant .ofEpochSecond(cursor.getLong(columnIndexes.dateModifiedColumn))
195198 val displayName = cursor.getString(columnIndexes.displayNameColumn)
196199 val absolutePath = cursor.getString(columnIndexes.dataColumn)
197200 val isTrashed =
198201 if (columnIndexes.isTrashed == null ) false else cursor.getInt(columnIndexes.isTrashed) == 1
199202
200- val relativePath = if (columnIndexes.relativePathColumn == null ) {
201- guessRelativePath(absolutePath, displayName)
202- } else {
203- cursor.getString(columnIndexes.relativePathColumn)
203+ try {
204+ val relativePath = if (columnIndexes.relativePathColumn == null ) {
205+ guessRelativePath(absolutePath)
206+ } else {
207+ cursor.getString(columnIndexes.relativePathColumn)
208+ }
209+ return MediaStoreFile (
210+ id,
211+ displayName,
212+ dateModified,
213+ absolutePath,
214+ relativePath,
215+ isTrashed
216+ )
217+ } catch (e: Exception ) {
218+ Log .w(TAG , e.toString())
219+ return null
204220 }
205-
206- return MediaStoreFile (
207- id,
208- displayName,
209- dateModified,
210- absolutePath,
211- relativePath,
212- isTrashed
213- )
214221 }
215222
216223 companion object {
0 commit comments