2013-01-17 5 views
6

Nel mio progetto ho un file:Webview Android loadDataWithBaseURL come caricare le immagini dalle risorse?

"MyProject/assets/folder1/image1.jpg" 
"MyProject/assets/folder1/index.html". 

In WebView Ho bisogno di aprire index.html (con immagini).

Ho provare questo codice:

Ma le immagini non si carica.

se ho messo le immagini su "beni" directory (MyProject/assets/) e rendono baseUrl = "file:///android_asset" immagini vengono caricate correttamente;

Come caricare le immagini non solo dalla directory delle risorse radice, ma e da assets/folder1?

+0

Ho visto un post lungo su questo argomento qualche tempo fa. Se hai ancora bisogno di aiuto, cerca. http://devblog.morethanheroic.com/2017/01/03/how-to-create-an-app-from-a-static-website/ –

risposta

-6

Hai dato il permesso di internet?

<uses-permission android:name="android.permission.INTERNET" /> 
+0

Seriamente? se dice che metterli in una cartella funziona bene significa che deve aver dato il permesso. – Darpan

8

provare come questo

WebView webview = (WebView)this.findViewById(R.id.webview); 


String html = "<html><head><title>TITLE!!!</title></head>"; 
html += "<body><h1>Image?</h1><img src=\"icon.png\" /></body></html>"; 


webview.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html", "UTF-8", null); 

Per ulteriori informazioni provare questo link

perfetta LoadDataWithBaseurl

+0

ho sentito che 'baseURL' di questa funzione ignorerà tutto ciò che viene dopo il primo /. – Darpan

1

prova di come questo

try { 
      String filePath = null; 
      filePath = "Your File path"; 
      Bitmap bitmap = null; 

      bitmap = BitmapFactory.decodeFile(filePath); 
      Log.v("Image data-->", "" + bitmap); 
      imageWidth = bitmap.getWidth(); 
      imageHeight = bitmap.getHeight(); 
      Log.e("Width", "" + imageWidth); 
      filePath = "file://" + filePath; 
      String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html\";charset=utf-8\"/><title></title></head><body style=\"width:" 
        + imageWidth 
        + "px; height:" 
        + imageHeight 
        + "px; background:url(" 
        + filePath 
        + ") no-repeat; position:relative;\">" 
        + getDivTag(mapList) 
        + "</body></html>"; 

      Log.v("MIS", "" + html); 
      webview.getSettings().setSupportZoom(true); 
      webview.loadDataWithBaseURL(null, html, "text/html", "utf-8", null); 

      System.out.println(html); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
4

Credo che bisogna impostare la base per le attività e aggiungere le sottocartelle per la tua immagine src di simile a questo:

webView.loadDataWithBaseURL("file:///android_asset/", readAssetFileAsString("folder1/index.html"), "text/html", "UTF-8", null);

Html: <img src="folder1/image1.jpg">

Questo ha funzionato per me su Android 5.1

private String readAssetFileAsString(String sourceHtmlLocation) 
{ 
    InputStream is; 
    try 
    { 
     is = getContext().getAssets().open(sourceHtmlLocation); 
     int size = is.available(); 

     byte[] buffer = new byte[size]; 
     is.read(buffer); 
     is.close(); 

     return new String(buffer, "UTF-8"); 
    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 

    return ""; 
} 
+0

Dov'è il metodo 'readAssetFileAsString'? – zygimantus

+1

aggiunto codice metodo, questo codice è stato precedentemente preso da un altro post SO, ma sfortunatamente non ho il link per accreditarli – behelit