When calling createNewFile() in android/java, why do I get: java.io.IOException: No such file or directory

huangapple go评论78阅读模式
英文:

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() + &quot;/RNDAlexaData/&quot;;
    Log.d(&quot;rootpath&quot;, rootPath);
    File root = new File(rootPath);
    if (!root.exists()) {
        root.mkdirs();
    }
    File f = new File(rootPath + &quot;test.pcm&quot;);
    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&#160;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 -->

&lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; android:maxSdkVersion=&quot;18&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;

答案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(), &quot;RNDAlexaData&quot;);
    Log.d(&quot;parent&quot;, dataFolder.getParent());

    if (!dataFolder.exists()) {
        Log.d(&quot;mkdir_success&quot;, &quot;Succesfully created directory: &quot; + dataFolder.mkdirs());
    }
    else {
        Log.d(&quot;fileexists&quot;, &quot;true&quot;);
    }

    File testMediaFile = new File(dataFolder, &quot;test.pcm&quot;);
    if (testMediaFile.exists()) {
        testMediaFile.delete();
    }
    Log.d(&quot;media_file&quot;, &quot;successfully created: &quot; + testMediaFile.createNewFile());

    FileOutputStream out = new FileOutputStream(testMediaFile);
    // TODO
    out.flush();
    out.close();

    return testMediaFile;
}

huangapple
  • 本文由 发表于 2020年8月12日 12:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63369582.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定