Repository: expo/expo
File: packages/expo-media-library/android/src/main/java/expo/modules/medialibrary/next/objects/asset/factories/AssetModernFactory.kt (Kotlin)

Code before:
      buildUniqueDisplayName(filePath)
    } else {
      filePath.lastPathSegment ?: "asset"
    }
    val path = relativePath ?: RelativePath.create(mimeType)

    var pendingUri: Uri? = null
    try {
      pendingUri = contentResolver.insertPendingAsset(displayName, mimeType, path)
      ensureActive()
      contentResolver.copyUriContent(filePath, pendingUri)
      ensureActive()
      contentResolver.publishPendingAsset(pendingUri)
      return@withContext create(pendingUri)
    } catch (e: Exception) {
      pendingUri?.let {
        contentResolver.delete(it, null, null)
      }
      // It occurs when trying to create too many assets with the same filename in the same album.
      // By default, the Content Resolver can resolve this issue for up to 32 assets, but then it throws this exception.
      val isCollisionError = e.message?.contains("Failed to build unique file", ignoreCase = true) == true
      if (isCollisionError && !forceUniqueName) {
        return@withContext createAssetInternal(filePath, relativePath, forceUniqueName = true)
      }
      throw e
    }
  }
}


Review comment (bug):
```suggestion
    val pendingUri = contentResolver.insertPendingAsset(displayName, mimeType, path)
    return@withContext try {
      ensureActive() 
      contentResolver.copyUriContent(filePath, pendingUri)
      ensureActive()
      contentResolver.publishPendingAsset(pendingUri)
      create(pendingUri)
    } catch (e: Exception) {
      contentResolver.delete(pendingUri, null, null)

      // It occurs when trying to create too many assets with the same filename in the same album.
      // By default, the Content Resolver can resolve this issue for up to 32 assets, but then it throws this exception.
      val isCollisionError = e.message?.contains("Failed to build unique file", ignoreCase = true) == true
      if (isCollisionError && !forceUniqueName) {
        createAssetInternal(filePath, relativePath, forceUniqueName = true)
      } else {
        throw e
      }
    }
```

Code after:
      buildUniqueDisplayName(filePath)
    } else {
      filePath.lastPathSegment ?: "asset"
    }
    val path = relativePath ?: RelativePath.create(mimeType)

    val pendingUri = contentResolver.insertPendingAsset(displayName, mimeType, path)
    return@withContext try {
      ensureActive()
      contentResolver.copyUriContent(filePath, pendingUri)
      ensureActive()
      contentResolver.publishPendingAsset(pendingUri)
      create(pendingUri)
    } catch (e: IllegalStateException) {
      contentResolver.delete(pendingUri, null, null)
      // It occurs when trying to create too many assets with the same filename in the same album.
      // By default, the Content Resolver can resolve this issue for up to 32 assets, but then it throws this exception.
      val isCollisionError = e.message?.contains("Failed to build unique file", ignoreCase = true) == true
      if (isCollisionError && !forceUniqueName) {
        createAssetInternal(filePath, relativePath, forceUniqueName = true)
      } else {
        throw e
      }
    }
  }
}