Android learning notes-save Files)

Source: Internet
Author: User

Android learning notes-save Files)
Android devices have two types of file storage areas: internal storage and external storage ). This name comes from early Android, when most Android devices provided two storage methods: built-in non-Loss memory (internal storage) and removable storage such as micro SD card (external storage ). Some devices divide permanent memory into two parts: internal and external, so even if there is no external storage, there are still two types of storage space. No matter whether there is external storage, the API method is the same. Internal Storage: Always available files can only be accessed and uninstalled by your app by default, the system deletes all files of your app from the internal storage. The internal storage is suitable for external storage of files that you do not want users or other apps to access: not always available (the user may connect the external storage in USB mode and will remove it from the device in some cases) is globally readable (world-readable ), therefore, some files may be read and uninstalled without control. Only the files you stored in getExternalFilesDir () are deleted () the file external storage in the directory is applicable to files that do not require storage restrictions, files that you want to share with other apps, or files that are accessible by your computer. The app is installed in internal storage by default, by specifying the value of the android: installLocation attribute, the app can be installed in external storage. Get external storage permissions: Read and Write: <manifest...> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/>... </manifest> Read: <manifest...> <uses-permission android: name = "android. permission. READ_EXTERNAL_STORAGE "/>... </manifest> you do not need any permissions to store files in internal storage. Your app always has read and write permissions in internal storage. Save the File in the internal storage: Get the appropriate Directory: getFilesDir () app file in the internal storage directory eg: File file = new File (context. getFilesDir (), filename); getCacheDir () app temporary cache files in the internal storage directory call openFileOutput () to get FileOutputStream write files to the internal directory eg: copy code 1 String filename = "myfile"; 2 String string = "Hello world! "; 3 FileOutputStream outputStream; 4 5 try {6 outputStream = openFileOutput (filename, Context. MODE_PRIVATE); 7 outputStream. write (string. getBytes (); 8 outputStream. close (); 9} catch (Exception e) {10 e. printStackTrace (); 11} copy Code call createTempFile () cache some files: Copy code 1 public File getTempFile (Context context, String url) {2 File file; 3 try {4 String fileName = Uri. parse (url ). getLastPathSegment (); 5 file = File. createTempFile (fileName, null, context. getCacheDir (); 6 catch (IOException e) {7 // Error while creating file 8} 9 return file; 10}

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.