3

I get html snippets updates from a network request and need to insert them into a local html file that can then be fetched with webView.loadUrl at runtime.

I can easily load the file from /android_assets/myFile.html using webView.loadUrl, but I cannot write to the file because the /android_assets directory is not accessible at runtime: Writing to /android_assets at runtime

So my question is, is there another location I can place the myFile.html so I can write to it at runtime and load it into the webView too?

Jupiter
  • 615
  • 1
  • 6
  • 15

2 Answers2

1

To do this, you should load content from html, modify it, and save it to storage.

Usage: String yourData = LoadData("myFile.html");

public String LoadData(String inFile) {
        String tContents = "";

    try {
        InputStream stream = getAssets().open(inFile);

        int size = stream.available();
        byte[] buffer = new byte[size];
        stream.read(buffer);
        stream.close();
        tContents = new String(buffer);
    } catch (IOException e) {
        // Handle exceptions here
    }

    return tContents;    
 }

To load your data in WebView. Call loadData() method of WebView

webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");
JunJie Wang
  • 460
  • 3
  • 10
  • This does not precisely answer the question because `getAssets()` method still reaches into `/android_assets` directory. I was looking for other location for HTML file that changes. However, your answer was still very helpful! – Jupiter Aug 28 '18 at 15:19
0

The asset directory is read only, you can not change it dynamically.
You can read & white files in the flowing directories:

Context.getFilesDir()  

No permission required
Typical position: /data/data/app.package.name/files/

Context.getExternalFilesDir(null)  

Requires WRITE_EXTERNAL_STORAGE when api level <= 18
Typical position: /sdcard/Android/data/app.package.name/files/

Environment.getExternalStorageDirectory()  

Requires WRITE_EXTERNAL_STORAGE
Typical position: /sdcard/

Run
  • 2,148
  • 2
  • 12
  • 29