How to load a URL that is inside a .txt by Web View?

2

It's the following, I'm making an application that is a kind of webBrowser. In this application, I have a url written inside a .txt, and I need to load that URL into my WebView. Here's what I've done:

NOTE: I can already load a URL into my WebView by specifying it in the apk code itself as you can see in the code below.

activity_main.xml:

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="50px"
    android:layout_height="50px"
    android:id="@+id/button"
    android:background="@drawable/engrenagem"
    android:layout_alignWithParentIfMissing="false"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_margin="20px" />

MainActivity.java:

package com.abacoti.awser;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;  ;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    String webURL = "http://www.google.com.br/";
    WebView web;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        web = (WebView) findViewById(R.id.webview);
        if (savedInstanceState == null)
        {
            web.loadUrl(webURL);
        }
        WebSettings webSettings = web.getSettings();
        webSettings.setJavaScriptEnabled(true);
        web.setWebViewClient(new WebViewClient());
    }

    @Override
    protected void onSaveInstanceState(Bundle outState )
    {
        super.onSaveInstanceState(outState);
        web.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        web.restoreState(savedInstanceState);
    }

    @Override
    public void onBackPressed() {
    }
}
    
asked by anonymous 15.02.2016 / 18:44

1 answer

3

If the URL is within a TXT you will need to read this TXT first using, of course the file should only contain the URL.

Based on this response link you should do this (if the file is in SD):

File sdcard = Environment.getExternalStorageDirectory();

//Pega o arquivo de text
File file = new File(sdcard, "MEU_ARQUIVO_TXT_COM_A_URL.txt");

//Pra obter a string
StringBuilder text = new StringBuilder();

try {
    InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF8");
    BufferedReader in = new BufferedReader(isr);
    String line;

    while ((line = in.readLine()) != null) {
        text.append(line);
    }
    in.close();
} catch (IOException e) {
    Log.d("TAG", "Erro de leitura do arquivo");
}

web = (WebView) findViewById(R.id.webview);
web.loadUrl(text.toString());

If it is in the assets folder of the application:

//Equivale ao android_asset/MEU_ARQUIVO_TXT_COM_A_URL.txt
InputStream inputStream = getAssets().open("MEU_ARQUIVO_TXT_COM_A_URL.txt");

//Pra obter a string
StringBuilder text = new StringBuilder();

try {
    InputStreamReader isr = new InputStreamReader(inputStream, "UTF8");
    BufferedReader in = new BufferedReader(isr);
    String line;

    while ((line = in.readLine()) != null) {
        text.append(line);
    }
    in.close();
} catch (IOException e) {
    Log.d("TAG", "Erro de leitura do arquivo");
}

web = (WebView) findViewById(R.id.webview);
web.loadUrl(text.toString());
  

The AP has changed the meaning of the question, so I have scratched the previous answer, for any similar problem I recommend trying this answer link

If the file is in the assets folder you have to load it like this:

WebView view = new WebView(this);
view.loadUrl("file:///android_asset/foo.txt");
setContentView(view);

If you want to access other files you need to use:

  • Returns the path of the first card (or other type of external storage):

     Environment.getExternalStorageDirectory().toString();
    
  • Return the system path (as Android is a linux you probably have something like / and you will not be able to write to most folders):

     Environment.getRootDirectory().toString();
    

Then the path should look something like:

String path = Environment.getExternalStorageDirectory().toString() + "/pasta/foo.txt";

WebView view = new WebView(this);
view.loadUrl(path);
setContentView(view);

Follow the documentation:

16.02.2016 / 18:44