Android files saved to the app and SD card

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" >1. Permissions add <uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>< Uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>public static String Getdatafolderpath (Context paramcontext) {return environment.getdatadirectory () + "/data/" + Paramco    Ntext.getpackagename () + "/files";    public static String Getmyfiledir (context context) {return Context.getfilesdir (). toString ();    public static String Getmycachedir (context context) {return Context.getcachedir (). toString (); }/** * @desc save content to file * @param filename * @param content * @throws Exception */public static V OID Save (context context, string fileName, string content, int module) {try {fileoutputstream os = con            Text.openfileoutput (fileName, module);            Os.write (Content.getbytes ());        Os.close (); } catch (Exception e) {e.Printstacktrace (); }}/** * @desc read file contents * @param fileName * @return */public static String read (Context Conte            XT, String FileName) {try {FileInputStream FIS = context.openfileinput (fileName);            Bytearrayoutputstream BOS = new Bytearrayoutputstream ();            Byte[] B = new byte[1024];            int len = 0;            while (len = Fis.read (b))! =-1) {bos.write (b, 0, Len);            } byte[] data = Bos.tobytearray ();            Fis.close ();            Bos.close ();        return new String (data);        } catch (Exception e) {e.printstacktrace ();    } return null;  /** * @desc Save the text to the file in the SD card * @param context * @param fileName * @param content * @throws IOException */public static void Savetosdcard (context context, string fileName, String content) throws Ioexceptio n{File File = new file (environment. getExternalStorageDirectory (), fileName);        FileOutputStream fos = new FileOutputStream (file);        Fos.write (Content.getbytes ());    Fos.close (); }/** * @desc read SD card file contents * @param fileName * @return * @throws IOException */public static Strin G Readsdcard (String fileName) throws IOException {File File = new file (Environment.getexternalstoragedirec        Tory (), fileName);        FileInputStream fis = new FileInputStream (file);        Bytearrayoutputstream BOS = new Bytearrayoutputstream ();        byte[] buffer = new byte[1024];        int len = 0;        while (len = fis.read (buffer))! =-1) {bos.write (buffer, 0, Len);        } byte[] data = Bos.tobytearray ();        Fis.close ();                Bos.close ();    return new String (data); }</span>


Android files saved to the app and SD card

Related Article

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.