M
M
Maksim_Demidov2015-05-24 11:56:26
Java
Maksim_Demidov, 2015-05-24 11:56:26

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);
}

Code for reading from raw resources:
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();
}

Tried a lot of file reading methods suggested by internet resources and in particular on stackoverflow english questions . There were one or two errors for each method, and half did not suit me at all, since there was a method for reading and opening already one file, when I need to read all files, almost the same as what I have now, but from a folder assets.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim_Demidov, 2015-05-24
@Maksim_Demidov

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 question

Ask a Question

731 491 924 answers to any question