Reputation: 13
I'm attempting to read a specific file from a specific folder on android that will then be read into an SQLite database. The reading into the database is working fine when I grab it from the assets folder(But this won't work for the final build since the file will change at time). However I'm having constant issues with file not found for actually accessing the file.
On the device the path for the file is shown as, "/storage/emulated/0/Wellpro" and the file name is "orfice.txt".
Currently my code to access the file is thrown together from about 3 or 4 different sources since I haven't found anything for this specifically somehow.
String fileName = "/storage/emulated/0/Wellpro/orfice.txt";
String path = Environment.getExternalStorageDirectory()+fileName;
InputStream is = new FileInputStream(path);
BufferedReader buffer = new BufferedReader(new InputStreamReader(is, "UTF-8"));
Any help would be greatly appreciated.
Thanks.
Upvotes: 0
Views: 90
Reputation: 16
Your first mistake is that filename should be "orfice.txt" since Environment.getExternalStorageDirectory() gives you the path. Follow @Kathi's solution which is good.
If you are still facing a problem, make sure you added the required proper permission to access external storage. If you are developing for an API <23, add to the Android Manifest the following permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
If you are developing for API = 23, you should request the permission at run-time. Check https://developer.android.com/training/permissions/requesting.html for more information on how to do it.
Upvotes: 0
Reputation: 1071
getAbsolutePath() give you full path of SD card.
try
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "myFile.txt";
// check if file path exist then read the file
File f = new File(baseDir + File.Separator + fileName);
FileInputStream fiStream = new FileInputStream(f);
Upvotes: 1