[英]Android Read Text File Into Dialog
Hi so i have an app that downloads a text file from my server and what i want to then do is show a dialog that only has a message that is written in dynamically by the text file so for example say i have a text file with the following 嗨,我有一个应用程序,可以从服务器上下载文本文件,然后我要执行的操作是显示一个对话框,该对话框仅包含由文本文件动态写入的消息,例如,说我有一个带有以下
This is my text file that contains some message and was downloaded from my server
这是我的文本文件,其中包含一些消息并已从我的服务器下载
Now I want to create a simple dialog like this 现在,我想创建一个像这样的简单对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(<text from file goes here>)
.setCancelable(true)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel
}
}).show();
Any help on how i can read the text file and then write it into my dialog as a message would be greatly appreciated thanks for any help or suggestions 非常感谢您对我如何阅读文本文件然后将其作为消息写入对话框的任何帮助,感谢您的任何帮助或建议
Ended up figuring out my problem and did it like this 最终弄清楚了我的问题并做到了
Dialog 对话
AlertDialog.Builder builder = new AlertDialog.Builder(Activity.this);
try {
builder.setMessage(readFile(context.getFilesDir().getAbsolutePath() + "/filename"))
.setCancelable(true)
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
}).show();
} catch (IOException e) {
Toast.makeText(Activity.this, "Error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Method 方法
private static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
return Charset.defaultCharset().decode(bb).toString();
}
finally {
stream.close();
}
}
Thanks for the help as always 一如既往的感谢您的帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.