英文:
When calling createNewFile() in android/java, why do I get: java.io.IOException: No such file or directory
问题
We attempted to create a file in Android by obtaining a path, establishing a new directory, and then generating a file within that directory. However, we encountered a runtime error. Here is the provided code and its output:
Code:
private File createTestFile() throws IOException {
String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/RNDAlexaData/";
Log.d("rootpath", rootPath);
File root = new File(rootPath);
if (!root.exists()) {
root.mkdirs();
}
File f = new File(rootPath + "test.pcm");
if (f.exists()) {
f.delete();
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.flush();
out.close();
return f;
}
Output:
D/rootpath: /storage/emulated/0/RNDAlexaData/
D/Response error: java.io.IOException: No such file or directory
W/System.err: java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
at java.io.File.createNewFile(File.java:1008)
at aut.rnd.alexa.ui.account.AccountFragment$TokenListener.createTestFile(AccountFragment.java:318)
The Android Manifest includes these permissions:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
英文:
We have tried to create a file in android by getting a path, making a new directory and then creating a file in that directory, but we get an error on runtime.
Here is the code:
<!-- language: java -->
private File createTestFile() throws IOException {
String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/RNDAlexaData/";
Log.d("rootpath", rootPath);
File root = new File(rootPath);
if (!root.exists()) {
root.mkdirs();
}
File f = new File(rootPath + "test.pcm");
if (f.exists()) {
f.delete();
}
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
out.flush();
out.close();
return f;
}
Output from android:
<!-- language: java -->
D/rootpath: /storage/emulated/0/RNDAlexaData/
D/Response error: java.io.IOException: No such file or directory
W/System.err: java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
at java.io.File.createNewFile(File.java:1008)
at aut.rnd.alexa.ui.account.AccountFragment$TokenListener.createTestFile(AccountFragment.java:318)
Android Manifest has these permissions:
<!-- language: xml -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案1
得分: 1
Environment.getExternalStorageDirectory()
已被弃用,请使用mContext.getExternalFilesDir(null).getAbsolutePath()
代替。
private File createTestFile() throws IOException {
File dataFolder = new File(mContext.getExternalFilesDir(null).getAbsolutePath(), "RNDAlexaData");
Log.d("parent", dataFolder.getParent());
if (!dataFolder.exists()) {
Log.d("mkdir_success", "成功创建目录: " + dataFolder.mkdirs());
}
else {
Log.d("fileexists", "true");
}
File testMediaFile = new File(dataFolder, "test.pcm");
if (testMediaFile.exists()) {
testMediaFile.delete();
}
Log.d("media_file", "成功创建: " + testMediaFile.createNewFile());
FileOutputStream out = new FileOutputStream(testMediaFile);
// TODO
out.flush();
out.close();
return testMediaFile;
}
英文:
Fixed my own problem with the help of googling and @blackapps & @Andreas in the comments.
<!-- language: java -->
Environment.getExternalStorageDirectory()
was deprecated, use <!-- language: java -->mContext.getExternalFilesDir(null).getAbsolutePath()
instead.
private File createTestFile() throws IOException {
File dataFolder = new File(mContext.getExternalFilesDir(null).getAbsolutePath(), "RNDAlexaData");
Log.d("parent", dataFolder.getParent());
if (!dataFolder.exists()) {
Log.d("mkdir_success", "Succesfully created directory: " + dataFolder.mkdirs());
}
else {
Log.d("fileexists", "true");
}
File testMediaFile = new File(dataFolder, "test.pcm");
if (testMediaFile.exists()) {
testMediaFile.delete();
}
Log.d("media_file", "successfully created: " + testMediaFile.createNewFile());
FileOutputStream out = new FileOutputStream(testMediaFile);
// TODO
out.flush();
out.close();
return testMediaFile;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论