尝试从 SDCard 读取时出现 FileNotFoundException
FileNotFoundException while trying to read from SDCard
我在 android phone sdcard 中通过 PC 中的文件浏览器创建文本文件。然后我尝试在 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>
我尝试了 运行 应用程序断开与电脑的连接,但它也没有工作并显示错误。可能是一个简单的问题。谁能解释我哪里出错了?
尝试一下。在日志中打印以下内容并告诉我们路径是什么:sdcard.getPath()
。
此外,使用 ES 文件资源管理器之类的应用程序查看您的 sample.txt
文件的 属性 并获取完整路径。它们匹配吗?
如果路径匹配,替换为:
File file = new File(sdcard,"sample.txt");
有了这个:
File file = new File("full_path_of_the_file");
这将帮助您排除问题所在。
祝你好运。
我在 android phone sdcard 中通过 PC 中的文件浏览器创建文本文件。然后我尝试在 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>
我尝试了 运行 应用程序断开与电脑的连接,但它也没有工作并显示错误。可能是一个简单的问题。谁能解释我哪里出错了?
尝试一下。在日志中打印以下内容并告诉我们路径是什么:sdcard.getPath()
。
此外,使用 ES 文件资源管理器之类的应用程序查看您的 sample.txt
文件的 属性 并获取完整路径。它们匹配吗?
如果路径匹配,替换为:
File file = new File(sdcard,"sample.txt");
有了这个:
File file = new File("full_path_of_the_file");
这将帮助您排除问题所在。
祝你好运。