在运行时写入android资产目录?

在运行时写入android资产目录?,android,android-webview,android-assets,Android,Android Webview,Android Assets,我从网络请求中获取html片段更新,需要将它们插入本地html文件中,然后在运行时使用webView.loadUrl获取该文件 我可以使用webView.loadUrl从/android\u assets/myFile.html轻松加载文件,但我无法写入文件,因为运行时无法访问/android\u assets目录: 因此,我的问题是,是否有其他位置可以放置myFile.html,以便在运行时写入并将其加载到webView中?要做到这一点,您应该从html加载内容,修改它,并将其保存到存储器中

我从网络请求中获取html片段更新,需要将它们插入本地html文件中,然后在运行时使用
webView.loadUrl
获取该文件

我可以使用
webView.loadUrl
/android\u assets/myFile.html
轻松加载文件,但我无法写入文件,因为运行时无法访问
/android\u assets
目录:


因此,我的问题是,是否有其他位置可以放置
myFile.html
,以便在运行时写入并将其加载到
webView
中?

要做到这一点,您应该从html加载内容,修改它,并将其保存到存储器中

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;    
 }
在WebView中加载数据。调用WebView的loadData()方法

webView.loadData(yourData, "text/html; charset=utf-8", "UTF-8");

资产目录为只读,不能动态更改。
您可以读取流动目录中的白色文件(&W):

Context.getFilesDir()  
无需许可
典型位置:/data/data/app.package.name/files/

Context.getExternalFilesDir(null)  
Environment.getExternalStorageDirectory()  

api级别时需要写入外部存储这不能准确回答问题,因为
getAssets()
方法仍然到达
/android\u assets
目录。我正在寻找HTML文件更改的其他位置。不过,你的回答还是很有帮助的!