android - 如何下载私人文件夹中的文本文件并在文本视图中显示文本文件内容
问题描述
我想从服务器的私人文件夹中下载文本文件,用户应该无法访问。简而言之,我只想将该文件存储在应用程序文件夹中,而不是存储卡/内存中。下载完成后,我想在文本视图中显示文本文件。如果文件已下载或存在于文件夹中,则只想显示该文件内容。有人能帮助我吗?提前致谢。
这是代码 -
url = new URL("https://tmpfiles.org/dl/11433/baby_shark_dodo.txt");
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.connect();
connection.setRequestMethod("GET");
try {
File mydir = getApplicationContext().getDir("Songs", Context.MODE_PRIVATE);
if(!mydir.exists()){
mydir.mkdir();
}
final File file = new File(mydir,"Test.txt");
FileOutputStream fileOutputStream = new FileOutputStream(file);
InputStream inputStream = connection.getInputStream();
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
fileOutputStream.write(buffer, 0, bufferLength);
}
fileOutputStream.close();
connection.disconnect();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
解决方案
推荐阅读
- android - 如何通过在另一个片段中单击按钮来刷新一个片段的 RecyclerView
- json - 如何在本机反应中解析json数据
- javascript - 防止父元素功能 onclick 子元素
- xslt - XSLT 换行元素和后续兄弟文本
- perl - Perl 日志消息意外写入 dest 文件
- python-3.x - 使用flask-socketio的gevent上的ConnectionAbortedError
- python - 如何在熊猫中编写一个包含 10cr 行和 10 列的巨大 csv
- php - 使用 PHP REST API 从 Wowza Streaming Engine 创建、更新和检索数据时遇到问题?
- c++ - 如果抛出异常,alloca() 是否会返回内存?
- azure - 从 azure 控制台应用程序作为 Web 作业记录