Answer the question
In order to leave comments, you need to log in
HTML from raw to assets + reading those files?
Good day to all! So. I have a ListView with many items, on clicking a certain one, I load a certain html file from the raw folder . But now I have a need to move all the files from the raw folder to the assets folder , but I don’t know what I need to change in the code for the correct reading from the assets folder (reading (not opening) exactly all files for the subsequent opening of the desired one), based on my code . Code:
In onCreate method
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setSupportZoom(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
Intent intent = getIntent();
//получаем строку и формируем имя ресурса
String resName = "file:///android_asset/" + "file" + intent.getIntExtra("head", 0);
Log.i("name", resName);
Context context = getBaseContext(); //получаем контекст
//читаем текстовый файл из ресурсов по имени
String text = readRawTextFile(context, getResources().getIdentifier(resName, "raw", "my_package_name"));
webView.loadDataWithBaseURL("file:///android_asset/", text, "text/html", "utf-8", null);
}
public static String readRawTextFile(Context context, int resId)
{
InputStream inputStream = context.getResources().openRawResource(resId);
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader buffReader = new BufferedReader(inputReader);
String line;
StringBuilder builder = new StringBuilder();
try {
while (( line = buffReader.readLine()) != null) {
builder.append(line);
builder.append("\n");
}
} catch (IOException e) {
return null;
}
return builder.toString();
}
Answer the question
In order to leave comments, you need to log in
Decided to do minimalism. Happened! Much, much easier than I thought.
To hell with it, I deleted everything after the line:
And made it look like this:
webView.loadUrl("file:///android_asset/" + resName + ".html");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question