Abd*_*man 5 java android file-permissions android-permissions android-studio
有一个应用程序生成的文本文件,我想将该文件作为字符串读取到我的应用程序中。我怎样才能做到这一点,任何帮助将不胜感激。这两个应用程序都是我的应用程序,因此我可以获得权限。
谢谢!
您可以将资产文件夹中的文本文件保存到 SD 卡中的任何位置,然后您可以从其他应用程序读取该文件。
此方法使用 getExternalFilesDir,它返回主共享/外部存储设备上的目录的绝对路径,应用程序可以在其中放置其拥有的持久文件。这些文件是应用程序的内部文件,通常作为媒体对用户来说不可见。
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(Environment.getExternalStorageDirectory(), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
Run Code Online (Sandbox Code Playgroud)
并阅读:
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5435 次 |
| 最近记录: |