简体   繁体   中英

How to convert HTML file to plain text and share it via WhatsApp, Facebook, and e.t

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM