Search code examples
androidfilefile-readfile-not-found

Android Choosing Specific File Location


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.


Solution

  • 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);