please somebody tell me what is the folder of sdcard and how can i create files in it.because i am new to android and i have googled so much but could not find any comprehensive stuff.i want to create file in sdcard manually. please help.
here is my code i have written but now it says fileNotFoundException. hence i have created a file in s开发者_StackOverflowdcard but still it is not recognisizing the file.any suggestions please.
try {
String root = android.os.Environment.getExternalStorageDirectory().getPath();
File gpxfile = new File(root, "sijjeel.txt");
//FileWriter writer = new FileWriter(gpxfile);
FileOutputStream writer = new FileOutputStream(gpxfile);
writer.write(bArray, 0, bArray.length);
writer.flush();
writer.close();
}
thanks alot
Path to sdcard is:
android.os.Environment.getExternalStorageDirectory().getPath()
To write a file, you can use the regular java.io.File methods for that.
For example, for creating a text files I use a helper method like this:
/**
* Stores text content into a file
* @param filename Path to the output file
* @param content Content to be stored in file
* @throws IOException
*/
public void storeFile(final String filename, final String content, String charSet)
throws IOException {
if (charSet==null) charSet = "utf-8";
Writer w = new OutputStreamWriter( new FileOutputStream(filename), charSet );
w.write(content);
w.flush();
w.close();
}
public void storeFile(final String filename, final String content)
throws IOException {
storeFile(filename, content, null);
}
or copying a file to sdcard:
public static final void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied to " + f2.getAbsolutePath());
} catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
} catch(IOException e){
System.out.println(e.getMessage());
}
}
It's /sdcard
(if you actually have a card in it)
The code you provided will create the file if it is not already there. Make sure you run your program on the emulator that has an SD card mounted. If it doesn't you can see an icon in the notification area saying so.
精彩评论