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;

    }

Monday 12 August 2013

Random Number generator

/*
* Takes Max and Min value and returns a random no
*/
 public static int getRandomValue(int min, int max) {
        Random foo = new Random();
        int randomNumber = foo.nextInt(max - min) + min;
        if (randomNumber == min) {
           
            return min + 1;
        } else {
            return randomNumber;
        }
    }