我可以创建文件。它是在/data/data/com.mypackage.app/files/myfile.txt上创建的。但我想在Internal Storage / Android / data / com.mypackage.app / files / myfiles.txt位置创建。我怎样才能做到这一点?
代码:
public void createFile() {
File path = new File(this.getFilesDir().getPath());
String fileName = "myfile.txt";
String value = "example value";
File output = new File(path + File.separator + fileName);
try {
FileOutputStream fileout = new FileOutputStream(output.getAbsolutePath());
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(value);
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
更新:
我解决了这个问题。也许有人帮忙。只改变这一行。
File output = new File(getApplicationContext().getExternalFilesDir(null),"myfile.txt");
答案 0 :(得分:0)
您可以使用以下方法获取根目录:
File path = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
您可以使用DIRECTORY_PICTURES
或null
,DIRECTORY_MUSIC
,DIRECTORY_PODCASTS
,DIRECTORY_RINGTONES
,DIRECTORY_ALARMS
而不是DIRECTORY_NOTIFICATIONS
。 ,DIRECTORY_PICTURES
或DIRECTORY_MOVIES
。
在此处查看更多信息: