2011-08-24 84 views
0

這個問題已經在論壇中被問了很多次,但在我的代碼中,我無法顯示我的圖像。我認爲這是不正確的方法:用html繪製本地圖像(WebView)

webViewContact.loadData(db.getParametres().get(0).getInformationParam(), "text/html", "utf-8"); 

getInformationParam()recup的HTML代碼,如:

<img src=\\"file:///android_asset/logoirdes_apropos.jpg\\"/> <b>Test</b> 

我的圖像文件是可繪製,我怎麼能顯示呢?

+0

通過這個[鏈接](http://stackoverflow.com/questions/3069822/is-it-possible-to-display-image-with-loaddatawithbaseurl-method-in-android)希望你會取得一些東西。日Thnx。 – user370305

回答

0

對loadData()可以做到的HTML有限制。建議使用loadUrl:

webViewContact.loadUrl("file:///android_asset/" + db.getParametres().get(0).getInformationParam()) 

你可以試試下面的代碼,你的文件將在:htmlFile。你現在可以在UI線程中做到這一點,但是如果文件很大,你可能會考慮在實際生產中將它移到一個AsyncTask。

String directory = Environment.getExternalStoragePublicDirectory("html_cache"); 
Writer output; 
    try { 
     directory.mkdir(); 
     File htmlFile = new File(directory + File.separator + "give_a_name.html"); 
     String content = db.getParametres().get(0).getInformationParam(); 
     // assumes default encoding is OK! 
     output = new BufferedWriter(new FileWriter(htmlFile)); 
     output.write(aContents); 
    } 
    finally { 
     output.close(); 
    } 
+0

不,getInformationParam返回html代碼,它不是html頁面,只是代碼。任何其他想法? – toshiro92

+0

你可以寫入共享目錄作爲一個文件,並嘗試從那裏加載它? – dongshengcn

+0

我直接從數據庫中獲得html代碼,根據我的培訓規範,我不想在沒有創建文件之前應用此代碼,但是如果我應該...我該怎麼做呢? – toshiro92