我正在尝试使用以下代码从sdcard读取文件
f = new RandomAccessFile("/storage/sdcard0/trac.txt", "r");
但是应用程序在崩溃的同时崩溃了。我能够在SD卡中创建和编写文件,但无法读取它。 我在清单中有以下权限,我将其作为系统应用程序运行:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_SUPERUSER"></uses-permission>
<uses-permission android:name="android.permission.INJECT_EVENTS"></uses-permission>
答案 0 :(得分:-1)
尝试通过以下方式获取SD卡路径:
String yourpath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/folderName/";
答案 1 :(得分:-1)
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String fileStr= "";
String aBuffer = "";
while ((fileStr= myReader.readLine()) != null) {
aBuffer += fileStr+ "\n";
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
"Done reading SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
对我有帮助。 更详细: