Android Data Storage

Source: Internet
Author: User
Android supports four data storage methods: Preference, file, database, content provider . A test at work these days Program I want to store an image file. I have a little bit of experience using the file method.

 

   First, preference, file, and Database correspond to/data/directories/Package name/Shared_pref,/data/Package name/Files,/data/Package name/Database.

 

   In Android, the file storage method is usually context. openfileoutput (string filename, int mode) and context. openfileinput (string filename ).

 

    Context. openfileoutput (string filename, int mode) files are automatically stored in/data/Package nameIn the/Files directory, the full path is/data/Package name/Files/Filename.Note that the filename parameter here cannot contain path delimiters (such "/").

 

   Generally, files generated in this way can only be accessed in this APK. However, this conclusion refers to the use of context. openfileinput (string filename. In this way, each APK can only access its/data/Package nameThe file in the/Files directory is very simple. The filename parameter cannot contain path delimiters. Android will automatically/Package name/Files directory to find the file named filename.

 

   However, if you use this file directly, this file can be accessed in other APK files, but you must call context before. do not use the default mode when using openfileoutput (string file, int mode:Mode_privateInstead, useMode_world_readable. The permission for the file generated using the default mode is "660" (that is, RW-RW ----), and the permission for using the latter to generate the file is allowed to run other APK access.CodeAs follows:

    File file = new file ("/data/package name/files/FILENAME ");

 

   Another way is to change the permission of the file to be generated. You can directly execute Linux commands in Java code. After all, Android is also Linux. The Code is as follows:

  // Process = runtime.getprocess(cmd.exe C ("chmod 666/data/package name/files/FILENAME ");
  //Process. waitfor ();

Process = runtime.getruntime(cmd.exe C ("ls");
bufferedreader ins = new bufferedreader (New inputstreamreader (process. getinputstream ();
string line;
while (line = ins. readline ())! = NULL)
{< br> toast. maketext (this, line, Toast. length_long). Show ();
}

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.