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

Code before:
    try {
      newAssetUri = contentResolver.insertPendingAsset(displayName, getMimeType(), relativePath)
      ensureActive()
      contentResolver.copyUriContent(contentUri, newAssetUri)
      ensureActive()
      contentResolver.publishPendingAsset(newAssetUri)

      val newAssetDelegate = AssetModernDelegate(
        newAssetUri,
        assetDeleter,
        mediaStorePermissionsDelegate,
        contextRef.getOrThrow()
      )
      return@withContext Asset(newAssetDelegate)
    } catch (e: Exception) {
      newAssetUri?.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 copyInternal(relativePath, forceUniqueName = true)
      }
      throw e
    }
  }
}


Review comment (bug):
```suggestion
    val newAssetUri = contentResolver.insertPendingAsset(
      displayName, 
      getMimeType(), 
      relativePath
    )

    return@withContext try {
	    ensureActive()
      contentResolver.copyUriContent(contentUri, newAssetUri)
      ensureActive()
      contentResolver.publishPendingAsset(newAssetUri)

      val newAssetDelegate = AssetModernDelegate(
        newAssetUri,
        assetDeleter,
        mediaStorePermissionsDelegate,
        contextRef.getOrThrow()
      )
      
      Asset(newAssetDelegate)
    } catch (e: Exception) {
      contentResolver.delete(newAssetUri, 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) {
        copyInternal(relativePath, forceUniqueName = true)
      } else {
        throw e
      }
    }
```

Code after:
      getMimeType(),
      relativePath
    )
    return@withContext try {
      ensureActive()
      contentResolver.copyUriContent(contentUri, newAssetUri)
      ensureActive()
      contentResolver.publishPendingAsset(newAssetUri)
      val newAssetDelegate = AssetModernDelegate(
        newAssetUri,
        assetDeleter,
        mediaStorePermissionsDelegate,
        contextRef.getOrThrow()
      )
      Asset(newAssetDelegate)
    } catch (e: IllegalStateException) {
      contentResolver.delete(newAssetUri, 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) {
        copyInternal(relativePath, forceUniqueName = true)
      } else {
        throw e
      }
    }
  }
}