Java 为什么.txt文件不能保存在内部存储器中?
我可以使用以下代码创建和共享.txt文件:Java 为什么.txt文件不能保存在内部存储器中?,java,android,directory,saving-data,Java,Android,Directory,Saving Data,我可以使用以下代码创建和共享.txt文件: File path = new File(Environment.getExternalStorageDirectory(), "Folder"); if (!path.exists()) { path.mkdirs(); } File exportFile = new File(path, fileName); FileWriter writer = new FileWriter(exportFile); writer.append(bod
File path = new File(Environment.getExternalStorageDirectory(), "Folder");
if (!path.exists()) {
path.mkdirs();
}
File exportFile = new File(path, fileName);
FileWriter writer = new FileWriter(exportFile);
writer.append(body);
writer.flush();
writer.close();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(exportFile));
startActivity(intent);
但是,我希望此文件保存到应用程序的文件夹中,而不是直接保存在SD卡上,因此我更改为下面的代码。它似乎不起作用,因为它没有创建文件,我尝试导出到的任何应用程序都看不到文件
File path = new File(this.getFilesDir(), "exports");
if(!path.exists()){
path.mkdir();
}
有什么想法吗?我试过的每一件事都不管用。文件应该保存在Android/data/[package name]文件夹中是吗
编辑:好的,我无法让它工作,但实际上发现这不是我想要使用的方法,我想将文件保存在Android/data文件夹中,为此我需要使用:
getExternalFilesDir(null)
检查
并确保您在清单文件中是否具有必要的权限
检查AndroidManifest.xml中的权限
检查目录可用性
检查外部存储的可用性
检查文件名。它不能包含空格
检查是否具有在该文件夹中写入的权限
您正在使用的文件构造函数将导出解释为文件名,而不是文件夹名。不确定这是否导致了您的问题。
ContextWrapper contextWrapper = new ContextWrapper(context);
File file = contextWrapper.getDir("media", Context.MODE_PRIVATE);