2011-08-24 40 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