Tuesday 13 August 2013

Read file from SD card in Android


Use this function to read files from SD card. Pass the path of the file with file name 
  -  ("/TestFolder/TestFile.text")

public static BufferedReader readFileFromSdCard(String fileName){
        File sdcard = Environment.getExternalStorageDirectory();
        File file=new File(sdcard,fileName);
              BufferedReader myReader = null;
        try {
            if (file.exists()){
            FileInputStream iStream =  new FileInputStream(file);
             myReader = new BufferedReader(
                    new InputStreamReader(iStream));
             Log.i("File", fileName+" Exists ");
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Log.e("File", fileName+" Not found");
            e.printStackTrace();
        }
        return myReader;

    }

No comments:

Post a Comment