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 {
Fileroot= Environment.getExternalStorageDirectory();
if (root.canWrite()){
Filegpxfile=newFile(root, "gpxfile.gpx");
FileWritergpxwriter=newFileWriter(gpxfile);
BufferedWriterout=newBufferedWriter(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{
Filef=newFile(Environment.getExternalStorageDirectory()+"/filename.txt");
fileIS = newFileInputStream(f);
BufferedReaderbuf=newBufferedReader(newInputStreamReader(fileIS));
StringreadString=newString();
//just reading each line and pass it on the debuggerwhile((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:
FileOuputStreamfos=newFileOutputStream( "/sdcard/" + FILENAME );
It will create a file in a root folder of your SD card.
Post a Comment for "Android Failed To Show Result"