Skip to content

Load images from persistentdatapath #224

Open

Description

@wakatanka

Hi, i load my html from an sqlite database on iOS, and all the images relative to this HTML in downloaded on the device in the persistentdatapath.
Using

string ImageFolderPath = "file:/" + Application.persistentDataPath.Replace("/", "//").Replace(" ", "%20") + "/manualimages/";
WebViewObject.LoadHTML(myHtml, ImageFolderPath);

The html il loaded correctly, but i can't see any images, are a there a workaround for this?

thanks

Activity

  1. KojiNakamaru commented on Jul 25, 2017

    @KojiNakamaru
    Member

    Replace("/", "//") will replace every / in the string so it should be removed. The following modification works for UIWebView (enableWKWebView:false).

    diff --git a/sample/Assets/Scripts/SampleWebView.cs b/sample/Assets/Scripts/SampleWebView.cs
    index 9ec7973..f8ab5dc 100644
    --- a/sample/Assets/Scripts/SampleWebView.cs
    +++ b/sample/Assets/Scripts/SampleWebView.cs
    @@ -104,7 +104,10 @@ public class SampleWebView : MonoBehaviour
                     }
                     System.IO.File.WriteAllBytes(dst, result);
                     if (ext == ".html") {
    -                    webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
    +                    //webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
    +                    webViewObject.LoadHTML(
    +                        System.Text.Encoding.UTF8.GetString(result),
    +                        "file://" + Application.persistentDataPath.Replace(" ", "%20") + "/");
                         break;
                     }
                 }

    However, WKWebView forbits file:// URLs for loadHtmlString:baseURL: so it won't load your images. For avoiding the issue, please put the main html under Application.persistentDataPath and call LoadURL() as done in the sample app.

  2. derwaldgeist commented on Jun 8, 2020

    @derwaldgeist

    Hi, I just stumbled upon this.

    Does it mean that this package won't work with file:// URLs embedded in a web page as well, unless you switch to UIWebView? I am asking because WKWebView is required by Apple nowadays.

    There is a flag to tell WKWebView that it may allow this:
    https://bugs.webkit.org/show_bug.cgi?id=154916#c7

    Is this implemented in this package?

    Also wondering how the situation is for Android 10.

    (We are currently using UniWebView and have problems with exact this. We need access to a local file that is actually a screen shot of the native Augmented Reality camera. It seems as if this worked in UniWebView before, but now is broken. I was wondering if we could use your package instead.)

  3. KojiNakamaru commented on Jun 8, 2020

    @KojiNakamaru
    Member

    This plugin just utilizes native Android WebView and/or iOS WKWebView/UIWebView components. The allowFileAccessFromFileURLs seems to work but is not officially supported.

    https://stackoverflow.com/questions/36013645/setting-disable-web-security-and-allow-file-access-from-files-in-ios-wkwebvi/41266699#41266699

    However, as described above, WKWebView (since iOS9) allows to load local files if you use LoadURL() instead of LoadHTML(). The sample app in this repository actually loads a local sample.html by LoadURL(), and sample.js and sample.jpg, both are referred in sample.html, are correctly loaded.

    https://github.com/gree/unity-webview/tree/ac10622fc470d1886444b9462eecd131f3469488/sample/Assets/StreamingAssets

    Please note the sample app first writes these files into Applicaiton.persistentDataPath and then load sample.html. You can therefore dynamically generate any webview content locally under Applicaiton.persistentDataPath for your webview.

    var exts = new string[]{
    ".jpg",
    ".js",
    ".html" // should be last
    };
    foreach (var ext in exts) {
    var url = Url.Replace(".html", ext);
    var src = System.IO.Path.Combine(Application.streamingAssetsPath, url);
    var dst = System.IO.Path.Combine(Application.persistentDataPath, url);
    byte[] result = null;
    if (src.Contains("://")) { // for Android
    #if UNITY_2018_4_OR_NEWER
    // NOTE: a more complete code that utilizes UnityWebRequest can be found in https://github.com/gree/unity-webview/commit/2a07e82f760a8495aa3a77a23453f384869caba7#diff-4379160fa4c2a287f414c07eb10ee36d
    var unityWebRequest = UnityWebRequest.Get(src);
    yield return unityWebRequest.SendWebRequest();
    result = unityWebRequest.downloadHandler.data;
    #else
    var www = new WWW(src);
    yield return www;
    result = www.bytes;
    #endif
    } else {
    result = System.IO.File.ReadAllBytes(src);
    }
    System.IO.File.WriteAllBytes(dst, result);
    if (ext == ".html") {
    webViewObject.LoadURL("file://" + dst.Replace(" ", "%20"));
    break;
    }
    }

    Android 10 seems to have many troubles about WebView, but these should be reduced by updating Android System WebView from Google Play.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions