Skip to content

Commit

Permalink
fix(android) Run the processing in a separate thread (#2142)
Browse files Browse the repository at this point in the history
Before this, the app's main thread was locked until all files
were copied and metadata were extracted. This means the
app's UI was not responsive.

Now the app's UI is responsive while those are processed, and
the JS Promise resolves once processed as expected.

This also prevents Application Not Responding alerts to
trigger when adding a lot of files at once.
  • Loading branch information
renchap committed Jun 15, 2023
1 parent 0c205c6 commit dc67109
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions android/src/main/java/com/imagepicker/ImagePickerModuleImpl.java
Expand Up @@ -18,6 +18,8 @@
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static com.imagepicker.Utils.*;

Expand Down Expand Up @@ -164,13 +166,17 @@ public void launchImageLibrary(final ReadableMap options, final Callback callbac
}

void onAssetsObtained(List<Uri> fileUris) {
try {
callback.invoke(getResponseMap(fileUris, options, reactContext));
} catch (RuntimeException exception) {
callback.invoke(getErrorMap(errOthers, exception.getMessage()));
} finally {
callback = null;
}
ExecutorService executor = Executors.newSingleThreadExecutor();

executor.submit(() -> {
try {
callback.invoke(getResponseMap(fileUris, options, reactContext));
} catch (RuntimeException exception) {
callback.invoke(getErrorMap(errOthers, exception.getMessage()));
} finally {
callback = null;
}
});
}

@Override
Expand Down

0 comments on commit dc67109

Please sign in to comment.