gpt4 book ai didi

java - 如何从assets文件夹中共享htm文本文件?

转载 作者:行者123 更新时间:2023-12-01 19:03:36 25 4
gpt4 key购买 nike

我已经使用下面的代码很长时间了,直到今天我尝试从 Assets 文件夹加载一些 html 文本文件。但不幸的是,共享 Intent 并没有奏效。因为我以前是用EXTRA_TEXT来声明的。现在我从 private static Final String URL="file:///android_asset/html_files/dancerkate.html";

读取了我的文件

请帮我更改此代码。这样它就可以共享 dancerkate.html 中的文本谢谢!


switch (item.getItemId()){

case R.id.share_button:
//share Copied Text characters via other apps button
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textView.getText().toString() + "Get App here: https://play.google.com/store/apps/details?id=com.joseph.workerbender");
sendIntent.setType("text/plain");
Intent.createChooser(sendIntent, "Share via");
startActivity(sendIntent);

}

return super.onOptionsItemSelected(item);


最佳答案

首先,当intentType为text时,无法使用intent发送文件

sendIntent.setType("text/plain");

您可以读取 .html 文件并获取该文件内的文本并通过共享发送

private String getTextFromAssets(String fileName){
try {
final int bufferSize = 1024;
final char[] buffer = new char[bufferSize];

InputStream inputStream = getResources().getAssets().open(fileName);

final StringBuilder out = new StringBuilder();
Reader in = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
int charsRead;
while((charsRead = in.read(buffer, 0, buffer.length)) > 0) {
out.append(buffer, 0, charsRead);
}
Log.d("HtmlText", out.toString());
return out.toString();
} catch (IOException e) {
Log.e("HtmlText", "HTML Read Error: "+e.toString());
}
return "";
}

现在从代码中调用该方法

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, getTextFromAssets("dancerkate.html"));
sendIntent.setType("text/plain");
Intent.createChooser(sendIntent, "Share via");
startActivity(sendIntent);

关于java - 如何从assets文件夹中共享htm文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59596453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com