Christopher Mederos
05/22/2024, 3:52 AMsuspendCancellableCoroutine { continuation ->
MyJavaTaskWithCallback() { myValueOnlyOnSuccess ->
continuation.resume(myValueOnlyOnSuccess)
}
}
Christopher Mederos
05/22/2024, 3:54 AMpublic static void scanFile(Context context, String[] paths, String[] mimeTypes,
OnScanCompletedListener callback) {
BackgroundThread.getExecutor().execute(() -> {
try (ContentProviderClient client = context.getContentResolver()
.acquireContentProviderClient(MediaStore.AUTHORITY)) {
for (String path : paths) {
final Uri uri = scanFileQuietly(client, new File(path));
runCallBack(context, callback, path, uri);
}
}
});
}
Sam
05/22/2024, 9:13 AMonly invokes the callback on successAre you sure? My reading of the code is that the callback will always be called exactly once for each item in the
paths
array, regardless of success or failure. In that case, you can just count the callbacks and stop when you've had the expected number. Are you seeing something different from that when you run the code?Sam
05/22/2024, 9:15 AMSam
05/22/2024, 9:15 AMsuspend fun scan(context: Context, paths: Array<String>): Map<String, Uri?> {
val results = callbackFlow {
MediaScannerConnection.scanFile(context, paths, null) { path, uri ->
trySend(path to uri)
}
}
return results.take(paths.size).toList().toMap()
}