There is a path to a specific file ( storage/sdcard/file.txt ). You need to open this file, read it, and put the data in it into the String string. How to do it?
1 answer
// Сохраняем директорию SD-карты в файл File sdcard = Environment.getExternalStorageDirectory(); // Берём текстовый файл из корня SD-карты // В этой переменной будет храниться наш файл File file = new File(sdcard, "file.txt"); // Читаем текст из файла StringBuilder text = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { text.append(line); text.append('\n'); } br.close(); } catch (IOException e) { // Обрабатываем ошибки } // Теперь в переменной 'text' будет храниться весь текст из файла Original: How can I read the SD card in Android?
Also, do not forget that in order to read / write to the SD card, you will need to register the necessary lines in the manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> And, as noted in the comments, you should also take care of the necessary access rights during the execution of the application (and not before installation) - you can read more, for example, here .
- You may also need a read permission from the user in runtime if the OS is> 6 - Yuriy SPb ♦
- @ YuriiSPb yes, I added about permissions :) - Peter Samokhin
- 3I meant not only this) On 6+ OS these permissions in the manifest are not enough to register) It is also necessary to check in runtime what they are and request if not) - YuriiSPb ♦
|