Android Database File Path Problems

Source: Internet
Author: User

// Obtain the dictionary. absolute path of the DB file <br/> string databasefilename = database_path + "/" + database_filename; <br/> file dir = new file (database_path ); <br/> // If the/sdcard/dictionary directory exists, create this directory <br/> If (! Dir. exists () <br/> dir. mkdir (); <br/> // If the directory does not exist in the/sdcard/dictionary directory <br/> // dictionary. DB file, copy the file from the res/raw directory to the <br/> // SD card directory (/sdcard/dictionary) <br/> If (! (New file (databasefilename )). exists () {<br/> // obtain the encapsulated dictionary. inputstream object of the DB file <br/> inputstream is = getresources (). openrawresource (<br/> r. raw. dictionary); <br/> fileoutputstream Fos = new fileoutputstream (databasefilename); <br/> byte [] buffer = new byte [8192]; <br/> int COUNT = 0; <br/> // Start copying dictionary. DB file <br/> while (COUNT = is. read (buffer)> 0) {<br/> FOS. write (buffer, 0, count); <br/>}</P> <p> FOS. close (); <br/> is. close ();

Android supports four data storage methods:Preference, file, database, content provider
. A test program that has been working over the past few days requires the storage of an image file. With this method, I have a little bit of experience.

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 name
In 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 name
The 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_private
Instead, 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. The Code is as 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 ();

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.