2

I have many HTML files in assets folder and I use jQuery mobile to show them in Android mobiles.I know it's little hard to answer this without any my code, but until now I didn't find a usable code to check it in Android studio, therefore I need it from you guys. Example, I have HTML files in assets folder, how can I convert it to plain text and share it via WhatsApp? There is code below, under example pictures, as I know Html.fromhtml can do this and and RegEx to remove HTML tags .toString().replaceAll("\<.*?>", "")); But I don't know how to realise it.

I know only something like this code can do it

public class MainActivity extends AppCompatActivity {

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    webView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = webView.getSettings();
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setDisplayZoomControls(false);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("file:///android_asset/about.html");
    webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    webView.setWebViewClient(new WebViewClient());
    webView.setWebChromeClient(new WebChromeClient());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    switch (item.getItemId())
    {
        case R.id.sharecontent:
            Share ();
            return true;
    }
    return super.onOptionsItemSelected(item);
  }

  public void Share() {
  this.Share(Html.fromHtml(this.web.getContent()).toString().replaceAll("\\<.*?>", ""));
  }

  public String getContent() {
    return this.m.ReadFile(GetUrl().substring(GetUrl().indexOf("asset/") + 6));
   }
}

Example of HTML file in emulator

(30.09.2017) I didn't get it work yet because I need access to HTML files inside subfolders. I read this, but still didn't understand - List of files in assets folder and its subfolders

Muhammad Tufail's answer works for only 1 HTML.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
alizulfuqar
  • 149
  • 1
  • 12
  • what exactly is your question – Aniruddha K.M Sep 29 '17 at 05:26
  • Example, i have html file in assets folder, how can i convert it to plain text and share it via WhatsApp? There is code above, as i know Html.fromhtml can do this and and regex .toString().replaceAll("\\<.*?>", "")); But i dont know how to realise it. – alizulfuqar Sep 29 '17 at 05:31

1 Answers1

1

First you need to read the file from the assets folder in android here is the example how to read html file from assets folder and get the text from the html file

 InputStream input;
 String output;
    try {
        StringBuilder buf=new StringBuilder();
        InputStream htmlfile=getAssets().open("yourfile.html");
        BufferedReader in=
                new BufferedReader(new InputStreamReader(htmlfile, "UTF-8"));
        String str;

        while ((str=in.readLine()) != null) {
            buf.append(str);
        }
         output=Html.fromHtml(buf.toString()).toString().trim();

        in.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

If you want to share the html text then you can share the text using android intent

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Sharing html Text");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, output);
startActivity(Intent.createChooser(sharingIntent, "Select"));
Muhammad Tufail
  • 326
  • 5
  • 14
  • Sorry, but itsnot working. it must be InputStream as i know and i can reach to assets folder, just cant reach text inside html – alizulfuqar Sep 29 '17 at 08:15
  • Very good, its working, good coding, thank you, but.... But, as i said, i have many **HTML files** in **assets folder** and these **html files** in separate **folders**. Example, **index.html** inside **index** folder, **about.html** inside **about** folder. i need to reach to all of these html files only with 1 coding. – alizulfuqar Sep 29 '17 at 12:46
  • okay all the file are in assets folder and subfolders and you want to get all that files. – Muhammad Tufail Sep 29 '17 at 12:55
  • Yes, exactly this. – alizulfuqar Sep 29 '17 at 13:01
  • yes this is possible but your need to pass the name of the directories (folders) – Muhammad Tufail Sep 29 '17 at 13:27
  • in subfolders i have too many html files, its mean i must write 1 by 1 all this html files in coding? – alizulfuqar Sep 29 '17 at 13:33