如何将文件存储在 android 的内部存储中以及如何在 android 手机中访问它们。我google了很多,发现我们可以存储文件,但只能通过模拟器或手机访问,必须root。
无根手机的解决方案是什么?
以及如何通过给出路径来访问内部存储文件?路径是什么?
请帮忙。提前致谢,我们将不胜感激。
如何将文件存储在 android 的内部存储中以及如何在 android 手机中访问它们。我google了很多,发现我们可以存储文件,但只能通过模拟器或手机访问,必须root。
无根手机的解决方案是什么?
以及如何通过给出路径来访问内部存储文件?路径是什么?
请帮忙。提前致谢,我们将不胜感激。
您可以通过 FileOutput 流执行此操作:
String fileName = "file.txt";
String content = "hello world";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
注意:这只是写入内部存储的方法,MODE_PRIVATE 意味着其他应用程序无法访问您的文件。您还需要在清单 xml 中授予 WRITE_EXTRENAL_STORAGE 权限。
这会将文件存储在:data/data/app/files/file.txt
要获取保存在存储中的文件列表,
File dirFiles = mContext.getFilesDir();
for (String fname: dirFiles.list())
{
Toast.makeText(mContext,fname,Toast.LENGTH_LONG).show();
}