1. Get the storage device directory:/sdcard (normally)
Sdpath=environment.getexternalstoragedirectory () +"/";
2. Determine if the folder on the SD card exists: through the Exists () method of the File object.
/* * * Determine if the document already exists; * /Public Boolean checkfileexists (String filepath) { file File=new file (sdpath+filepath); return file.exists (); }
3. Create a directory on the SD card: implemented by the mkdir () method of the File object.
/* * Create a directory on the SD card ;* /Public File Createdir (String dirpath) { file dir =new File (sdpath+dirpath); Dir.mkdir (); return dir;}
4. Create the file on the SD card: implemented by the CreateNewFile () method of the File object.
/* * Create file on SD card; */public file CreateFile (String filepath) throws ioexception{ File File=new file (sdpath+filepath); File.createnewfile (); return file; }
5. Write the InputStream byte stream to the SD card file .
/** * Write data from one InputStream to sd card*/ Publicfile Writestreamtosdcard (String dirpath,string filename,inputstream input) {file file=NULL; OutputStream Output=NULL; Try { //create a directory;Createdir (Dirpath); //create a file on the created directory;File = CreateFile (dirpath+filename); Output=Newfileoutputstream (file); byte[]bt=New byte[4*1024x768]; while(Input.read (BT)!=-1) {output.write (BT); } //Refresh the cache,Output.flush (); } Catch(IOException e) {e.printstacktrace (); } finally{ Try{output.close (); } Catch(Exception e) {e.printstacktrace (); } } returnfile; }
Files on Android SD card