El siguiente código es usado
mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setAllowFileAccess(true); mWebView.getSettings().setJavaScriptEnabled(true); String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String imagePath = base + "/test.jpg"; mWebView.loadUrl(imagePath);
la imagen no se está cargando …
también intenté
mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setAllowFileAccess(true); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String imagePath = base + "/test.jpg"; String html = "
"; mWebView.loadData(html, "text/html","utf-8");
Por favor ayuda
mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setAllowFileAccess(true); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.getSettings().setBuiltInZoomControls(true); String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String imagePath = "file://"+ base + "/test.jpg"; String html = "
"; mWebView.loadDataWithBaseURL("", html, "text/html","utf-8", "");
Esto hizo el truco ya que tenemos que agregar el archivo “prefijo”: // “antes de cualquier archivo para mostrar en la vista web
WebViews puede mostrar HTML, no imágenes. O necesita usar un ImageView o generar algo de HTML con una etiqueta de imagen que muestra su imagen. Si necesita ser dynamic, puede generarlo como una cadena y usar el método loadData () para mostrarlo.
Editar: Vas a querer algo como esto en tu html String.
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String imagePath = base + "/test.jpg"; String html = ("
"); mWebView.loadData(html, "text/html","utf-8");
Probé los métodos mencionados anteriormente sin éxito, así que estas son las opciones que me funcionaron para cargar una imagen desde el almacenamiento externo:
Cargue la imagen directamente en WebView.
Suposiendo que tengo una imagen llamada image.jpg
dentro de la raíz del directorio de almacenamiento externo (en mi caso /storage/emulated/0/image.jpg
).
String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String imagePath = pathExternalStorage + "/" + "imagen.jpg"; /* //We can chek if the file really exists. File archivo = new File(imagePath); if(archivo.exists()){ Log.i("TAG" , "EXISTS " + imagePath); }else{ Log.e("TAG" , "DOESN´T EXISTS " +imagePath ); } */ String imagePath = "file://" + imagePath; webView.loadUrl(imagePath);
Cargando la imagen usando una plantilla html para cargarla en WebView.
String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String imagePath = pathExternalStorage + "/" + "image.jpg"; String imagePathWV = "file://" + imagePath; String html = ("
"); webView.loadDataWithBaseURL(null, html, "text/html","utf-8",null);