I am trying to read a file from sdcard using the following code
f = new RandomAccessFile("/storage/sdcard0/trac.txt", "r");
But the app crashes while doing so. I am able to create and write a file in sdcard but unable to read it. I have following permissions in my manifest and i am running it as a system app:
<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>
Try getting your sd card path by doing this:
String yourpath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/folderName/";
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();
}
Its helpful for me. To more detail :
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.