Comment afficher le contenu HTML à la WebView en utilisant Android
-
26-09-2019 - |
Question
Voici mon contenu HTML que je souhaite afficher dans le WebView en utilisant Android SDK. Il affiche uniquement
//S'il vous plaît
Mais lorsque je mets ce contenu HTML dans le navigateur, cela montre différemment.
<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework
help help with homework homework assignments elementary school high school middle school
// --><font color="#60c000" size="4"><strong>Please!</strong></font>
Veuillez suggérer comment résoudre ce problème
J'ai un autre problème que dans le contenu HTML, il y a une balise
<img src="http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif" border="0" />
Dans ces images, ne montre pas.
La solution
- Utilisez web.loadDatawithbaseurl au lieu de web.loaddata (et n'oubliez pas d'échapper aux chaînes là où il est nécessaire)
- Vous devez ajouter une autorisation Internet pour télécharger des images et les visualiser dans votre fichier manifeste.
Cet exemple fonctionne pour moi:
public class SimpleMusicStream extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
WebView wv = (WebView) findViewById(R.id.WebView01);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = "<br /><br />Read the handouts please for tomorrow.<br /><br /><!--homework help homework" +
"help help with homework homework assignments elementary school high school middle school" +
"// --><font color='#60c000' size='4'><strong>Please!</strong></font>" +
"<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif' />";
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
}
Et n'oubliez pas d'ajouter:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Dans votre fichier AndroidManifest.xml
Autres conseils
Soit vous pouvez le faire comme ci-dessus, soit mettre un fichier html dans le dossier d'actifs et l'utiliser le loke pour l'afficher
view.loadUrl("file:///android_asset/FILENAME.html");
Pour l'affichage d'image, vous pouvez le faire comme
String str= " img src=\"http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif\" alt=\"this is img\"ALIGN=\"right\"/>"; wv.loadData(str, "text/html", "utf-8");
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow