简体   繁体   English

如何从Webview中的资源加载图片?

[英]how to load a picture from my resource in webview?

I want to load a picture called map.png set in my drawable resource in the WebView. 我想在WebView的可绘制资源中加载一张名为map.png的图片。

I found in another answer this suggestion webview.loadUrl("file://...") but I don't understand how to set it properly. 我在另一个答案中找到了这个建议webview.loadUrl(“ file:// ...”),但我不知道如何正确设置它。 I always get error that the requested file was not found. 我总是收到找不到所请求文件的错误。 This is what I wrote 这就是我写的

`public class Map extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
  WebView mWebView =(WebView)findViewById(R.id.webMap);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("file://res/drawable/map");`

This is the XML 这是XML

    <WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webMap"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">

</WebView>

在此处输入图片说明

     setContentView(R.layout.map);
  WebView mWebView =(WebView)findViewById(R.id.webMap);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("file:///android_assets/map)");

This is a complete screen shot 这是完整的屏幕截图

在此处输入图片说明

This is the new code but I get an error 这是新代码,但出现错误

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        WebView myWebView = (WebView) findViewById(R.id.webMap);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.loadUrl("file://mnt/sdcard/map.png");

You can access like this:- 您可以像这样访问:

file:///android_res/drawable/YOUR_FILE_NAME 文件:/// android_res / drawable / YOUR_FILE_NAME

you can not access Drawable from application's drawable directory like 您无法从应用程序的drawable目录访问Drawable

   mWebView.loadUrl("file://res/drawable/map");

for file from your asset directory of your app package you should use 对于应用程序包资产目录中的文件,应使用

   mWebView.loadUrl("content://"+Context.getResources().getAsset()+"filename");

EDIT: ok put your image in Applications asset directory, then for example write line, 编辑:好的,将您的图片放在“应用程序”资产目录中,然后例如写一行,

  myWebView.loadUrl("file:///android_asset/testimage.jpg"); 

or use 或使用

    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.loadUrl("file://mnt/sdcard/map.png");

try it 试试吧

试试看

mWebView.loadUrl("file://res/drawable/map.png")

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM