2017-01-22 59 views
0

需要幫助!如何將html頁面加載到android webview及其相關資源(如圖像和css文件)?

我所有的網絡資源,在 「/存儲/ sdcard0/Test」 的目錄,如下面的android:

  1. 所有的CSS文件,該目錄下有云:/存儲/ sdcard0 /測試/樣式

  2. 所有圖像文件進入這個目錄下:/存儲/ sdcard0 /測試/圖片

  3. 所有HTML這個目錄下:/存儲/ sdcard0 /測試/文本

Text目錄下的每個html文件都使用Styles和Images目錄中的資源。

我能夠將html頁面加載到web視圖中,但其關聯圖像不顯示在web視圖中。

這裏是我的代碼:

WebView webView = (WebView) findViewById(R.id.web_view); 

    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setAllowFileAccessFromFileURLs(true); 
    webView.getSettings().setAllowUniversalAccessFromFileURLs(true); 
    File storageDir = Environment.getExternalStorageDirectory(); 

    File baseUrl = new File(storageDir, "Test1"); 

    File text = new File(baseUrl, "Text"); 

    File file = new File(text, "part0013.xhtml"); 

    StringBuffer data = new StringBuffer(); 

    try { 
     BufferedReader bf = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 

     String line; 
     while((line = bf.readLine())!= null){ 
      data.append(line); 
     } 


    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    Log.d("MainActivity", baseUrl.getAbsolutePath().toString()); 

    webView.loadDataWithBaseURL(baseUrl.getAbsolutePath().toString(), data.toString() 
      , "text/html","UTF-8", null); 

下面是我的部分HTML內容:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> 
 
<title>Chapter 2. Understanding Views&#8212;The UI Building Blocks</title> 
 
<link rel="stylesheet" type="text/css" href="../Styles/style0001.css"/> 
 
<link rel="stylesheet" type="application/vnd.adobe-page-template+xml" href="page-template.xpgt"/> 
 
</head><body> 
 
<h2 id="ch02"><a id="page_23"></a>Chapter 2. Understanding Views&#8212;The UI Building Blocks</h2> 
 
<p class="noindent"><strong>Sometimes it is best to start with the building blocks before diving into much more complex topics, and that is the goal here. This chapter is all about views, which allow your apps to display their content to your users. You will learn all the major view subclasses and gain a fundamental understanding of key attributes such as view IDs, padding, and margins. If you have already built any apps, most of this will be review for you, so feel free to skim through the chapter as you move toward the heart of the book.</strong></p> 
 
<div class="heading"> 
 
<h3 id="ch02lev1sec1"><a id="page_24"></a>What Is a View?</h3> 
 
<p class="noindent">Views are the most basic component of the user interface, and they extend the <code>View</code> class. They always occupy a rectangular area (although they can display content of any shape) and can handle both drawing to the screen and events such as being touched. Everything that is displayed on the screen utilizes a view.</p> 
 
<div class="image"> 
 
<p class="tab-caption"><a id="ch02tab01"></a><strong>Table 2.1</strong> <code>View</code>&#8217;s Most Commonly Used Attributes</p> 
 
<div class="image"><img src="../Images/image00407.jpeg" alt="Image"/></div> 
 
<div class="image"><img src="../Images/image00408.jpeg" alt="Image"/></div> 
 
<img src="../Images/image00409.jpeg" alt="Image"/></div>"

有任何一點毛病此代碼?

+0

你需要分享你的HTML,包括CSS和圖像。根據[loadDataWithBaseURL](https://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL)上的文檔,您可能需要在正在構建的URL上使用文件協議。 –

+0

嗨,羅斯。不確定我明白嗎?我的html文件包含訪問其資源的相對URL。 –

回答

0

調試代碼後,最後我找到了解決方案,只需使用loadUrl()方法。

webView.loadUrl("file:///" + file.getAbsolutePath()); 

但是,爲loadDataWithBaseURL編寫的文檔幾乎沒有誤導性,實際上並沒有從基本目錄加載資源。就我而言,它指的是基目錄的父目錄。

相關問題