You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 8.0应用内部存储文件可列出但打开异常及文件无法打开问题

Android Internal Storage: Fixing "No such file or directory" When Opening Created Files

Hey there, let's figure out why you're seeing that frustrating "No such file or directory" error even though your file shows up in the file list.

The Root Cause

When you use openFileOutput() to create a file, it gets saved in your app's private internal storage directory (specifically /data/data/[your-app-package-name]/files/). The issue is likely that you're trying to open the file using just the filename "xxxyyy.txt" without referencing this full, app-specific path. The system can't locate the file if you don't point it to the correct directory.

How to Fix It

Instead of using just the filename, build the full file path using getFilesDir(), which returns the exact directory where your openFileOutput() files are stored. Here's how to adjust your code:

Correct File Creation (with proper error handling)

First, let's add try-catch blocks to catch any hidden creation issues:

FileOutputStream output = null;
try {
    // This creates the file in getFilesDir() automatically
    output = openFileOutput("xxxyyy.txt", Context.MODE_PRIVATE);
    String str = "Just any string";
    byte[] bytes = str.getBytes();
    output.write(bytes, 0, bytes.length);
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (output != null) {
            output.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Correct File Opening

When you want to read the file, use getFilesDir() to build the full path:

FileInputStream input = null;
try {
    // Build the full file path using getFilesDir()
    File file = new File(getFilesDir(), "xxxyyy.txt");
    input = new FileInputStream(file);
    
    // Example: Read the file content
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    StringBuilder content = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        content.append(line);
    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        if (input != null) {
            input.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Key Notes

  • Internal Storage is Private: Your app can only access its own internal storage files by default—this is normal behavior, so you don't need any special permissions (unlike external storage).
  • fileList() Explained: The fileList() method returns exactly the filenames stored in getFilesDir(), so seeing your file there confirms it was created successfully. The problem is always in how you're referencing the file when opening it.
  • Android 8.0 Specifics: Android 8.0 (API 26) doesn't change internal storage access rules, so this fix works across all devices including API 26 and above.

内容的提问来源于stack exchange,提问作者Agam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:07:16