Files on Android SD card

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.