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

Android开发中FileInputStream触发java.io.FileNotFoundException问题求助

解决Android中FileInputStream运行时崩溃的问题

Hey there! Let's figure out why your FileInputStream is causing crashes even though your code compiles fine and you've checked the string format. Here are the most common fixes based on Android's file system rules:

1. 检查文件路径是否符合Android存储规则

Android restricts access to certain file locations, especially on newer versions. Hardcoding absolute paths (like /sdcard/xxx.txt) often leads to issues because:

  • External storage paths can vary across devices (some use /storage/emulated/0/ instead of /sdcard/).
  • You might be trying to access files outside your app's private sandbox without proper permissions.

Fix: Use Android's built-in methods to get valid paths:

// 访问应用私有内部存储的文件
File privateFile = new File(getFilesDir(), "your_file.txt");
FileInputStream fis = new FileInputStream(privateFile);

// 访问应用私有外部存储的文件(Android 10以下需WRITE_EXTERNAL_STORAGE权限)
File externalPrivateFile = new File(getExternalFilesDir(null), "your_file.txt");
FileInputStream fis = new FileInputStream(externalPrivateFile);

2. 确认是否缺少必要的权限

If you're accessing files outside your app's private directory (like public external storage), you need to handle permissions properly:

  • Add the permission to your AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • For Android 6.0 (API 23) and above, request the permission dynamically at runtime:
private static final int READ_STORAGE_REQUEST = 101;

// 先检查权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
            READ_STORAGE_REQUEST);
    return; // 等待权限申请结果,不要继续执行文件操作
}

// 权限通过后再初始化FileInputStream
FileInputStream fis = new FileInputStream(yourFilePath);

3. 验证文件是否真实存在且可读取

Even if your path string looks correct, the file might be missing, or your app might not have read access to it. Add checks before opening the stream:

String filePath = "你的路径字符串";
File targetFile = new File(filePath);

if (!targetFile.exists()) {
    Log.e("FileError", "文件不存在: " + filePath);
    return;
}
if (!targetFile.isFile()) {
    Log.e("FileError", "路径指向的不是文件: " + filePath);
    return;
}
if (!targetFile.canRead()) {
    Log.e("FileError", "没有读取该文件的权限: " + filePath);
    return;
}

// 现在安全地打开输入流
FileInputStream fis = new FileInputStream(targetFile);

4. 不要用FileInputStream读取APK内置文件

If your file is in the assets/ or res/raw/ directory of your project, you can't access it via FileInputStream—these files are packed inside the APK, not in the device's file system. Use these methods instead:

// 读取assets目录下的文件
InputStream is = getAssets().open("asset_file.txt");

// 读取res/raw目录下的文件(文件会被分配R.raw.xxx的ID)
InputStream is = getResources().openRawResource(R.raw.raw_file);

Pro Tip: If you can share the exact exception message from your Logcat (like FileNotFoundException or SecurityException), we can narrow down the issue even faster!

内容的提问来源于stack exchange,提问作者T.Rode

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:26:08