Android - añadiendo una imagen en html webview
Ok, entonces la imagen que estoy usando se llama test.png y tengo una clase java (Cherry.java) y una clase xml (cherry.xml) También tengo un archivo html en la carpeta / res / raw llamado htmltest.html . Lo que estoy tratando de hacer es cuando el usuario hace clic en un botón de la página anterior y luego lo lleva a cherry.xml, todo es una vista web. Ahora en la clase java solo está abriendo el archivo htmltest y en el archivo html es solo un diseño normal basado en la web. Quiero mostrar imágenes en el archivo html, así que una imagen que esté en la carpeta dibujable o algo así sin tener que usar internet. (No quiero que se use el permiso de internet). A continuación se muestra el código de los 3 archivos que tengo.
cherry.xml
<WebView
android:id="@+id/webviewHelp"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Cherry.java
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Cherry extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cherry);
WebView webview = (WebView) findViewById(R.id.webviewHelp);
webview.loadData(readTextFromResource(R.raw.htmltest), "text/html", "utf-8");
}
private String readTextFromResource(int resourceID)
{
InputStream raw = getResources().openRawResource(resourceID);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int i;
try
{
i = raw.read();
while (i != -1)
{
stream.write(i);
i = raw.read();
}
raw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return stream.toString();
}
}
htmltest.html
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
<h2>Pictures</h2>
<img border="0" src="file:///android_drawable/test.png" alt="nothing" width="304" height="228" />
</body>
</html>
Si tiene más preguntas, solo pregunte y le responderé lo más rápido posible. Todo funciona bien, solo las imágenes que no puedo mostrar.