我在 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。
最佳答案
首先你需要从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"));
关于android - 如何将 HTML 文件转换为纯文本并通过 WhatsApp、Facebook 和 e.t 共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46482043/