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 ();
}