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

Asked

Viewed 420 times

2

The thing is, I’m making an app that’s kind of webbrowser. In this app, I have a url written inside a . txt, and I need to load this URL into my Webview. Follow what I’ve already done:

NOTE: I can already load a URL in my Webview by specifying it in the apk code itself as you can see in the codes 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/";
    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() {
    }
}
  • 3

    It’s not very clear what you want to do. You can better illustrate?

  • You want to load a page from a specified URL into a text file or you want to render what is in the text file inside the WebView?

  • The URL (LINK) is written inside the TXT file?

  • Exactly! I’ll edit the question!

  • I will re-open the question, but next avoid changing both the question with edits, especially when there are already answers. See you soon!

  • Thank you! Anyway, William had already responded and edited his reply.

Show 1 more comment

1 answer

3


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

Using this answer as the basis https://stackoverflow.com/a/12421888/1518921 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 you are in the application’s Assets folder:

//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 changed the meaning of the question, so I crossed off the previous answer, for any similar problem I recommend trying this answer /a/133955/3635

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();
    
  • Returns the system path (as Android is a linux you will probably have something like / and cannot save to most folders):

     Environment.getRootDirectory().toString();
    

Then the path must be something like:

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

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

Follows documentation:

  • Actually, it is a file (.txt) located on the device root.

  • becomes a white screen

  • All right, I’m here...

  • I put reward ok?

  • Since I will use the device’s own "HD", it would be System.out.print(Environment.getRootDirectory(). toString());?

  • William continues the white screen...

  • Are you talking about logcat? If yes, I can’t find anything related to that line.

  • Nothing yet, look at the way I’m putting it: web = (WebView) findViewById(R.id.webview);&#xA; System.out.print("meu log:" + Environment.getExternalStorageDirectory().toString());&#xA; String path = Environment.getExternalStorageDirectory().toString() + "/awser/url.txt";&#xA; WebView view = new WebView(this);&#xA; view.loadUrl(path);&#xA; setContentView(view);

  • How do I send you all the log around here? It’s pretty big!

  • Here! http://pastebin.com/jY6PT40Z

  • Right! I’ll get back to you tomorrow!

  • 1

    Thank you very much!

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.