Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Android WebView not displaying bundled assets #17304

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Map;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Picture;
Expand Down Expand Up @@ -462,7 +463,7 @@ public void setSource(WebView view, @Nullable ReadableMap source) {
return;
}
if (source.hasKey("uri")) {
String url = source.getString("uri");
String url = resolveSourceUri(view.getContext(), source.getString("uri"));
String previousUrl = view.getUrl();
if (previousUrl != null && previousUrl.equals(url)) {
return;
Expand Down Expand Up @@ -622,6 +623,23 @@ public void onNewPicture(WebView webView, Picture picture) {
return mPictureListener;
}

protected String resolveSourceUri(Context context, String sourceUri) {
if (sourceUri != null && !sourceUri.contains(":/")) {
// The source is just a string, so it's probably a bundled resource
// which was copied into the app resource folder by the react native packager
// so try to find it
String[] resTypes = {"raw", "drawable"};
for (String resType : resTypes) {
int id = context.getResources().getIdentifier(sourceUri, resType, context.getPackageName());
if (id != 0) {
return "file:///android_res/" + resType + "/" + sourceUri;
}
}
}

return sourceUri;
}

protected static void dispatchEvent(WebView webView, Event event) {
ReactContext reactContext = (ReactContext) webView.getContext();
EventDispatcher eventDispatcher =
Expand Down