2017-01-22 12 views
0

ここをクリックしてください!イメージやCSSファイルなどの関連リソースを使ってAndroidページをHTMLページに読み込む方法は?

私はアンドロイドでは以下のように "/ストレージ/ sdcard0 /テスト" ディレクトリ内のすべてのWebリソースを持っている:

  1. すべてのCSSファイルは、このディレクトリの下に行く:/ストレージ/ sdcard0 /テスト/スタイル

  2. すべての画像ファイルは、このディレクトリの下に行く:/ストレージ/ sdcard0 /テスト/画像

  3. このディレクトリの下にすべてのHTML:/ストレージ/ sdcard0 /テスト/テキスト

各テキストディレクトリの下のhtmlファイルは、Styles and 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

CSSと画像を含むHTMLを共有する必要があります。 [loadDataWithBaseURL](https://developer.android.com/reference/android/webkit/WebView.html#loadDataWithBaseURL)のドキュメントに従って、作成しているURLでファイルプロトコルを使用する必要があります。 –

+0

こんにちはC.ロス。わかりません?私のHTMLファイルには、そのリソースにアクセスするための相対URLが含まれています。 –

答えて

0

コードをデバッグした後、最後にloadUrl()メソッドを使用して解決する方法を見つけました。

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

しかし、loadDataWithBaseURLのために書かれた文書は、少し誤解を招くおそれがあり、実際にベースディレクトリからリソースをロードしません。私の場合は、ベースディレクトリの親ディレクトリを参照していました。

関連する問題