Android WebView将base64 url​​发送到Javascript(拒绝加载URL,因为它超过X个字符。)

时间:2022-09-04 08:19:23

I'm trying to send a base64 image to javascript but I keep getting this error in Android Studio:

我正在尝试将base64图像发送到javascript,但我在Android Studio中一直收到此错误:

W/chromium: [WARNING:navigator_impl.cc(280)] Refusing to load URL as it exceeds 2097152 characters.

W / chromium:[警告:navigator_impl.cc(280)]拒绝加载URL,因为它超过了2097152个字符。

I've tried using loadDataWithBaseURL but I can't figure out how to make it actually run any Javascript so I'm not sure if that's a solution.

我已经尝试使用loadDataWithBaseURL,但我无法弄清楚如何让它实际运行任何Javascript所以我不确定这是否是一个解决方案。

This is the code I'm using, it works for some images but some are too large and it gives me that error :(

这是我正在使用的代码,它适用于一些图像但有些太大而且它给了我那个错误:(

Thanks for any help!

谢谢你的帮助!

if (resultCode == RESULT_OK)
{
    Uri selectedImage = intent.getData();
    myWebView.loadUrl("javascript:setFileUri('" + selectedImage.toString() + "')");
    String path = getRealPathFromURI(this, selectedImage);
    //myWebView.loadUrl("javascript:setFilePath('" + path + "')");


    Bitmap bm = BitmapFactory.decodeFile(path);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
    byte[] b = baos.toByteArray();

    String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

    myWebView.loadUrl("javascript:setFilePath('" + encodedImage + "')");
}

1 个解决方案

#1


2  

Don't know if you found an answer to this yet or not, but I was struggling with the same thing all day trying to send an image to Javascript. As with you, loadUrl was giving me the '2097152' error, so I tried using evaluateJavascript but kept getting webview syntax errors. After playing around with it, I finally got it to work like this:

不知道你是否找到了这个问题的答案,但我整天都在努力将图像发送到Javascript。和你一样,loadUrl给了我'2097152'的错误,所以我尝试使用evaluateJavascript但仍然得到webview语法错误。玩完之后,我终于让它像这样工作:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

try {
    final String retFunction = "imageReturnFunction('data:image/png;base64," + URLEncoder.encode(encoded, "UTF-8") + "');";

    runOnUiThread(new Runnable() {
        public void run() {
            webView.evaluateJavascript(retFunction, null);
        }
    });
} catch(Exception ex) {
    ex.printStackTrace();
}

Note that I needed it to be formatted as a dataURL in my case (which would be pretty common in a JS app), but you can adjust the code if needed. Hope this helps.

请注意,在我的情况下,我需要将其格式化为dataURL(这在JS应用程序中很常见),但您可以根据需要调整代码。希望这可以帮助。

#1


2  

Don't know if you found an answer to this yet or not, but I was struggling with the same thing all day trying to send an image to Javascript. As with you, loadUrl was giving me the '2097152' error, so I tried using evaluateJavascript but kept getting webview syntax errors. After playing around with it, I finally got it to work like this:

不知道你是否找到了这个问题的答案,但我整天都在努力将图像发送到Javascript。和你一样,loadUrl给了我'2097152'的错误,所以我尝试使用evaluateJavascript但仍然得到webview语法错误。玩完之后,我终于让它像这样工作:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

try {
    final String retFunction = "imageReturnFunction('data:image/png;base64," + URLEncoder.encode(encoded, "UTF-8") + "');";

    runOnUiThread(new Runnable() {
        public void run() {
            webView.evaluateJavascript(retFunction, null);
        }
    });
} catch(Exception ex) {
    ex.printStackTrace();
}

Note that I needed it to be formatted as a dataURL in my case (which would be pretty common in a JS app), but you can adjust the code if needed. Hope this helps.

请注意,在我的情况下,我需要将其格式化为dataURL(这在JS应用程序中很常见),但您可以根据需要调整代码。希望这可以帮助。