【问题标题】:FileNotFoundException while trying to read from SDCard尝试从 SDCard 读取时出现 FileNotFoundException
【发布时间】:2016-09-20 12:36:39
【问题描述】:

我通过 pc 中的文件浏览器在我的 android 手机 sdcard 中创建文本文件。然后我尝试在android应用程序中阅读它。我收到FileNotFoundException - no such path or directory exist 错误。

这是我的代码

private String readFromFile(Context context) {

        StringBuilder text = new StringBuilder();
        String state = Environment.getExternalStorageState();

        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard,"sample.txt");
            //Read text from file
            try {
                //File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sample.txt");
                BufferedReader br = new BufferedReader(new FileReader(file));//Error occurs here
                String line;
                Toast.makeText(context, "success!", Toast.LENGTH_LONG).show();
                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
            }
            catch (IOException e) {
                e.getMessage();
                Toast.makeText(context, "Error!", Toast.LENGTH_LONG).show();
                //You'll need to add proper error handling here
            }
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
        } else {
// Something else is wrong. It may be one of many other states, but all we need
//  to know is we can neither read nor write
        }     
        return text.toString();
    }

sample.txt 存在于 sdcard 的根目录中(我已三重检查)。它有一些文字。

我已在清单文件中授予读取外部存储权限。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bulsy.graphapp">
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我尝试运行应用程序从 pc 断开连接,然后它也没有显示错误。可能是一个简单的问题。谁能解释我哪里出错了?

【问题讨论】:

  • 请再次检查您的内部存储,而不是您的辅助存储(sd 卡),sample.txt 是否可用。
  • 它必须在 sdcard 中吧?当我调试 BufferedReader 试图从 /storage/sdcard0/sample.txt 读取时导致 Filenotfound 异常

标签: android file bufferedreader sd-card


【解决方案1】:

尝试一下。在日志中打印以下内容并向我们展示路径是什么:sdcard.getPath()

另外,使用 ES File explorer 之类的应用程序查看您的 sample.txt 文件的属性并获取完整路径。它们匹配吗?

如果路径匹配,则替换:

File file = new File(sdcard,"sample.txt");

有了这个:

File file = new File("full_path_of_the_file");

这将帮助您消除问题所在。
祝你好运。

【讨论】:

  • 路径是'/storage/sdcard0/sample.txt'
  • 尝试以下操作。将getExternalStorageDirectory() 替换为getExternalStorageDirectory(null)。看看这是否有所作为
  • 路径不同! ES explorer 说是/storage/sdcard1/sample.txt!它奏效了!当我按照你说的改变它时!干杯!
  • 很高兴它成功了。请记住不要将其硬编码,因为路径的 sdcard1 部分可能因不同的 Android 设备而异
  • 我得到文件的完整路径为 '/sdcard/Books/x.pdf' 但 sdcard.path 给出 '/storage/emulated/0/x.pdf'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
相关资源
最近更新 更多