Skip to content Skip to sidebar Skip to footer

Android Failed To Show Result

In following code i want to crate a file in sdcard. but it not giving any valid output. showing only the hello world...Where the 'file created' message will be displayed and where

Solution 1:

try below code might help you

try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
    File gpxfile = new File(root, "gpxfile.gpx");
    FileWriter gpxwriter = new FileWriter(gpxfile);
    BufferedWriter out = new BufferedWriter(gpxwriter);
    out.write("Hello world");
    out.close();
}
}catch (IOException e) {
    Log.e(TAG, "Could not write file " + e.getMessage());
}

Note: For this to be working your emulator or device must have SDcard

Edit: For reading the file fromSDCard

try{

File f = new File(Environment.getExternalStorageDirectory()+"/filename.txt");
   fileIS = new FileInputStream(f);
   BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
   String readString = new String(); 
   //just reading each line and pass it on the debugger
   while((readString = buf.readLine())!= null){
      Log.d("line: ", readString);
   }
} catch (FileNotFoundException e) {
   e.printStackTrace();
} catch (IOException e){
   e.printStackTrace();
}

Solution 2:

Using this method you're trying to create a file in phone's internal memory.

Just use this code:

FileOuputStream fos = new FileOutputStream( "/sdcard/" + FILENAME );

It will create a file in a root folder of your SD card.


Post a Comment for "Android Failed To Show Result"