2011-04-28 16 views
9

He guardado un archivo en la carpeta raíz y estoy tratando de abrirlo en una vista web.Abrir archivo Html local en Webview - Android

Este es mi código para el ahorro:

OutputStream outstream = null; 
    outstream = openFileOutput(fileName ,MODE_WORLD_READABLE); 

    /// if file the available for writing 
    if (outstream != null) { 
     /// prepare the file for writing 
     OutputStreamWriter outputreader = new OutputStreamWriter(outstream); 
     BufferedWriter buffwriter = new BufferedWriter(outputreader); 

     /// write the result into the file 
     buffwriter.write(result); 
    } 

    /// close the file 
    outstream.close(); 

} catch (java.io.FileNotFoundException e) { 
    System.out.println("File not found in the writing..."); 
} catch (IOException e) { 
    System.out.println("In the writing..."); 
} 

Este es mi código para recordar el archivo:

    fileView.getSettings().setJavaScriptEnabled(true); 
      fileView.loadUrl("file:///" + name); <--- 

y dentro de la aplicación me da un archivo no se encuentra el error.

Cualquier idea es útil.

Respuesta

6

La ruta es incorrecta, suponiendo que las excepciones no se vieron afectadas. archivo

: /// indica al navegador para buscar/Nombre

openFileOutput (nombre del archivo) dice que la aplicación escriba en < aplicaciones-files-directorio >/filename

archivo Su URL debe ser" : /// "+ getFilesDir() + + File.separator nomArchivo

+0

Estoy pensando en empezar en la creación de juegos para Android. En teoría, ¿es posible para mí crear un lienzo html5 y un juego de javascript, almacenar los archivos localmente en el teléfono Android y luego abrirlos con vista web para permitir que el jugador juegue? – Jacob

15
WebView mWebView=(WebView)findViewById(R.id.mWebView); 

      mWebView.loadUrl("file:///book.html"); 
      mWebView.getSettings().setJavaScriptEnabled(true); 
      mWebView.getSettings().setSaveFormData(true); 
      mWebView.getSettings().setBuiltInZoomControls(true); 
      mWebView.setWebViewClient(new MyWebViewClient()); 

private class MyWebViewClient extends WebViewClient 
{ 
    @Override 
    //show the web page in webview but not in web browser 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl (url); 
     return true; 
    } 
} 

probar este

11

en realidad, cuando se abre un T RL usando file:///...
Entonces significa que debe guardar el archivo en el directorio de activos (por ejemplo, test.html). Ahora supongamos que tiene para acceder a test.html archivo, tiene que escribir como esto

loadURL("file:///android_asset/test.html'); 
+0

Quiero navegar de test.html a page1.html, estoy usando 'Page 1' en test.html porque ambos están en el mismo directorio, pero no funcionan – Sarz

Cuestiones relacionadas