Skip to content

Commit

Permalink
Fix Android WebView not displaying bundled resources
Browse files Browse the repository at this point in the history
This was seen in release builds see the following issues:
facebook#16133
facebook#7924
  • Loading branch information
jeanregisser committed Dec 21, 2017
1 parent 0b5e8b4 commit 90bbd61
Showing 1 changed file with 19 additions and 1 deletion.
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

0 comments on commit 90bbd61

Please sign in to comment.