【问题标题】:How to convert HTML file to plain text and share it via WhatsApp, Facebook, and e.t如何将 HTML 文件转换为纯文本并通过 WhatsApp、Facebook 和 e.t 共享
【发布时间】:2018-03-10 23:00:27
【问题描述】:

我在 assets 文件夹中有许多 HTML 文件,我使用 jQuery mobile 在 Android 手机中显示它们。我知道这并不难在没有任何我的代码的情况下回答这个问题,但直到现在我还没有找到一个可用的代码来在 Android Studio 中检查它,因此我需要你们。例如,我在 assets 文件夹中有 HTML 文件,如何将其转换为纯文本并通过 WhatsApp 共享?下面有代码,在示例图片下,据我所知 Html.fromhtml 可以做到这一点,并且 RegEx 可以删除 HTML 标记 .toString().replaceAll("\<.*?>", "")); 但我不知道如何实现它。

我知道只有这样的代码才能做到

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) 我还没有让它工作,因为我需要访问子文件夹中的 HTML 文件。我读了这个,但还是不明白 - List of files in assets folder and its subfolders

Muhammad Tufail 的答案仅适用于 1 个 HTML。

【问题讨论】:

  • 你的问题到底是什么
  • 例如,我在 assets 文件夹中有 html 文件,如何将其转换为纯文本并通过 WhatsApp 共享?上面有代码,因为我知道 Html.fromhtml 可以做到这一点,并且正则表达式 .toString().replaceAll("\\<.>", ""));但我不知道如何实现它。

标签: android html share file-conversion


【解决方案1】:

首先你需要从 android 中的 assets 文件夹中读取文件,这里是如何从 assets 文件夹中读取 html 文件并从 html 文件中获取文本的示例

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

如果你想共享 html 文本,那么你可以使用 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"));

【讨论】:

  • 抱歉,它不起作用。据我所知,它必须是 InputStream,我可以访问 assets 文件夹,只是无法访问 html 中的文本
  • 非常好,它的工作,良好的编码,谢谢,但是.... 但是,正如我所说,我在 assets 文件夹中有许多 HTML 文件 和这些 html 文件 在单独的 文件夹 中。例如,index.html 在 index 文件夹中,about.html 在 about 文件夹中。我只需要使用 1 个编码即可访问所有这些 html 文件。
  • 好的,所有文件都在 assets 文件夹和子文件夹中,您想要获取所有这些文件。
  • 是的,正是这个。
  • 是的,这是可能的,但您需要传递目录(文件夹)的名称
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2010-09-22
  • 2018-02-10
  • 1970-01-01
  • 2012-04-12
  • 2014-09-08
相关资源
最近更新 更多