简体   繁体   English

Android:无法从内部存储将图像加载到Webview

[英]Android: cannot load image to webview from internal storage

For my android app I want to load a html file to webview, this html file will be stored in "data/data/com.myapp/files/index.html". 对于我的Android应用程序,我想将html文件加载到webview,此html文件将存储在“ data / data / com.myapp / files / index.html”中。 I am able to load it with the associated javascript files but when I want to load the image from "data/data/com.myapp/files/img/image.png" it doesn't work. 我可以使用关联的javascript文件加载该文件,但是当我要从“ data / data / com.myapp / files / img / image.png”加载该图像时,它将无法正常工作。 This is how i'm trying to do it: 这就是我试图做到的方式:

webView.loadDataWithBaseURL(getFilesDir()+"/img/", html, "text/html","utf-8", ""); //html is my html string I get from index.html

I've also tried: 我也尝试过:

webView.loadUrl(getFilesDir()+"/img/"+index.html");

In my html file i have 在我的html文件中

 <img id="image" src="/img/image.png"/>

I've verified and the file is loaded on the disk but i can't display it on webview, the display stays blank for image. 我已经验证并且文件已加载到磁盘上,但是我无法在Webview上显示它,显示图像保持空白。 I just can't make it work. 我只是无法使其工作。 So my question is: is it even possible? 所以我的问题是:有可能吗? If yes can someone please tell me how? 如果可以,请告诉我如何?

I've searched and found that others had similar problems but none of the solutions is working. 我进行搜索后发现,其他人也有类似的问题,但是没有一种解决方案有效。

EDIT: Putting my files in asset directorty is not an option for me, which i've already tested and it works. 编辑:将我的文件置于资产管理权对我来说不是一个选择,我已经对其进行了测试并且可以正常工作。

I just tried the following code I found here: Load the image saved in sdcard in webview 我刚刚尝试了在这里找到的以下代码: 将保存在sdcard中的图像加载到webview中

String base = getFilesDir(); 
String imagePath = base + "/test.jpg"; 
String html = ("<html>
            <head>
            </head>
            <body>
            <img src=\""+ imagePath + "\">
            </body>
            </html>
            "); 
mWebView.loadData(html, "text/html","utf-8");

But still no success, apparently it worked for the person who asked the question. 但是仍然没有成功,显然它对提出这个问题的人有用。 I've also tested with the external storage but the result is same. 我还测试了外部存储,但结果是相同的。 All permissions are there in manifest.xml. 所有权限都在manifest.xml中。 So I really don't see what the problem is. 所以我真的不知道问题出在哪里。 Any suggestion or advice will be appreciated. 任何建议或意见将不胜感激。

Consider putting static files in assets folder inside android project, and try loading file using: 考虑将静态文件放入android项目内的assets文件夹,然后尝试使用以下方法加载文件:

mWebView.loadUrl("file:///android_asset/html/index.html");

And then, from HTML file refer to images, styles using relative path. 然后,从HTML文件使用相对路径引用图像,样式。

For example, in your case, you would have follwing directory structure: 例如,在您的情况下,您将具有以下目录结构:

PROJECT-ROOT
 |-src/
 |-assets/
    |-html/index.html
      |-img/image.png
      |-css/style.css

After this, you can use normal html tags inside index.html as you would do to make any web page. 之后,您可以像创建任何网页一样在index.html使用普通的html标签。 Eg. 例如。 <img id="image" src="img/image.png"/> or <link rel="stylesheet" type="text/css" href="css/style.css"> <img id="image" src="img/image.png"/><link rel="stylesheet" type="text/css" href="css/style.css">

Don't forget to enable JavaScript using: 不要忘记使用以下方法启用JavaScript:

WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

Problem solved. 问题解决了。 It was actually coming from bad formatted data, once I checked and added correct files to phone memory it started working. 实际上,它来自格式错误的数据,一旦我检查了正确的文件并将其添加到手机内存中,它便开始工作。

When you want to save image to internal storage, be sure not to use same code as downloading html file. 当您要将图像保存到内部存储器时,请确保不要使用与下载html文件相同的代码。 That's the mistake I made. 那是我犯的错误。 Instead, use 相反,使用

How to download and save an image in Android 如何在Android中下载和保存图像

to save your image, then the image should be fine 保存图像,那么图像应该没问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android WebView 从内部存储加载 | 安卓 11 | 安卓 R - Android WebView load from internal storage | Android 11 | Android R Android:将图像从本地存储加载到webview - Android: load image from local storage to webview 如何在android的webview中从内部存储加载图像? - How to load images from internal storage in webview in android? Android-将图像从Internet保存到内部/外部存储并在Webview中显示 - Android - Save image from internet to internal / external storage and display it in webview Kotlin 从内部存储加载 html 到 webview - Kotlin load html from internal storage to webview Android:无法从Android设备上的内部存储访问图像文件 - Android: Cannot access an image file from the internal storage on Android device 权限被拒绝,以便从Android内部存储加载图像 - Permission Denied in order to load Image from Android Internal Storage 从内部存储加载html文件(例如“ index.html”)到android webview - Load html file e.g “index.html” from internal storage to android webview 将图像文件从内部存储加载到arrayList - Load image files from internal storage into arrayList 无法从内部存储器将图像加载到ImageView中 - Unable to load image in a ImageView from the Internal Storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM