簡體   English   中英

Android:在Webview中顯示圖像

[英]Android : Display images in Webview

處理完文件后,我得到一個HTML圖像,其中圖像被設置為

<img src="abc.001.png" width="135" height="29" alt="" style="margin-left:0pt; margin-top:0pt; position:absolute; z-index:-65536" />

不應修改圖像的路徑,因為我必須從列表中選擇文件項。 該圖像與文件位於同一目錄中。 我使用loadData / loadDataWithBaseURL加載HTML字符串,但不顯示圖像。 我只看到它的框架。

我怎樣才能解決這個問題? 我可以應用該解決方案,以防我有許多圖像被索引為.001.jpg,.002.png等...(所有在目錄中)?

更新:謝謝,無論我如何命名圖像,它都適用於loadUrl()語句。 事實上,我必須在將內容加載到WebView之前閱讀並處理內容。 這就是我使用loadDataWithBaseUrl()語句並解決上述問題的原因。 這是我在測試項目中的代碼,用於讀取和顯示Test.html的內容。

    String res = "";        
    File file = new File(Environment.getExternalStorageDirectory()+"/Test.html");
    try {
        FileInputStream in = new FileInputStream(file);

        if (in != null) {               
            BufferedReader buffreader = new BufferedReader(
                    new InputStreamReader(in));
            String line;
            while ((line = buffreader.readLine()) != null) {
                res += line;
            }
            in.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }       
    wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
  //wv.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Test.html");

//中的語句有效,但這不是我在實際項目中可以做的。 我有一個解決方案:處理完內容后,我必須將其保存在一個臨時的HTML文件中然后加載它,該文件將在以后刪除。 但是,我仍然期待更好的解決方案:)

嘗試更改圖像文件的名稱。 我以為這是因為名字中有雙點。

<img id="compassRose" src="CompassRose.jpg"></img>

這對我有用....

碼:

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebView;

public class StackOverFlowActivity extends Activity {

    private Handler mHandler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView view=(WebView)findViewById(R.id.webView1);
        view.getSettings().setJavaScriptEnabled(true);
        view.loadUrl("file:///android_asset/index.html");
        view.addJavascriptInterface(new MyJavaScriptInterface(), "Android");
    }

    final class MyJavaScriptInterface
    {
        public void ProcessJavaScript(final String scriptname, final String args)
            {             
                mHandler.post(new Runnable()
                    {
                        public void run()
                            {
                                //Do your activities
                            }
                    });
            }
    }
}

index.html的:

<html>
<head>
  <title title="Index"></title>                                   
</head>

<body>
  <h2>Android App demo</h2>
  <br /> <img src="CompassRose.jpg" />
</body>
</html>

在此輸入圖像描述

結果:

在此輸入圖像描述

你的問題在於:

wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);

第一個參數(baseURL)為null。 出於某種原因,即使您使用絕對URL,WebView也拒絕加載任何鏈接的資源。 您可能會發現,如果您將代碼更改為(假設您的圖像存儲在外部目錄中),它將起作用:

wv.loadDataWithBaseURL ("file://" + Environment.getExternalStorageDirectory(), res, "text/html", "utf-8", null);

如果您在網絡上引用資源,請記住添加適當的權限:

<uses-permission android:name="android.permission.INTERNET" />

對不起,我的回復有點晚了。 當我遇到這篇文章時,我正在研究類似的問題。

只需使用:

webview.loadUrl("file:///android_asset/mypicture.jpg");
       WebView webView=(WebView)findViewById(R.id.my_wb);
        String url = "YOUR URL";
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(url);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM