이미지 및 CSS 파일과 같은 관련 리소스를 사용하여 HTML 페이지를 Android WebView에로드하는 방법은 무엇입니까?

사가르 라 쏘드

여기에 도움이 필요합니다 !!

나는 모든 웹 리소스가 아래의 안드로이드에서 "/ storage / sdcard0 / Test"디렉토리에 있습니다.

  1. 모든 CSS 파일은 / storage / sdcard0 / Test / Styles 디렉토리 아래에 있습니다.

  2. 모든 이미지 파일은 다음 디렉토리에 있습니다. / storage / sdcard0 / Test / Images

  3. 이 디렉토리 아래의 모든 html : / storage / sdcard0 / Test / Text

Text 디렉토리 아래의 각 html 파일은 Styles and Images 디렉토리의 리소스를 사용합니다.

html 페이지를 웹보기로로드 할 수 있지만 관련 이미지가 웹보기에 표시되지 않습니다.

내 코드는 다음과 같습니다.

    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>"

이 코드에 문제가 있습니까?

사가르 라 쏘드

코드를 디버깅 한 후 마침내 loadUrl () 메서드를 사용하여이를 수행하는 솔루션을 찾았습니다.

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

그러나 loadDataWithBaseURL 용으로 작성된 문서는 오해의 소지가 거의 없으며 실제로 기본 디렉토리에서 리소스를로드하지 않습니다. 제 경우에는 기본 디렉토리의 상위 디렉토리를 참조했습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관