Due to work needs, today we have studied how to create folders and modify their permissions in Android. We need to know that each application package has a private directory for storing data (similar to folders ), only applications of this package can write data to this directory space. The private data directory of each package application is in the absolute path of Android/data/<package name>/directory. In addition to the private data directory, the application also has the/sdcard directory (that is, the write permission of the SD card, but the access permission of the files under the SD card cannot be modified ). Third-party applications cannot be written to other system directories in the file system.
The Code is as follows:
1,
// Create a folder
File destdir = new file ("/data/[your path]/Temp ");
If (! Destdir. exists ()){
Destdir. mkdirs ();
}
// Modify permissions
Fileoutputstream Fos;
Fos = openfileoutput ("FILENAME", mode_world_readable );
Note: The available mode parameters are as follows:
/**
* File Creation Mode: the default mode, where the created file can only
* Be accessed by the calling application (or all applications sharing
* Same user ID ).
* @ See # mode_world_readable
* @ See # mode_world_writeable
*/
Public static final int mode_private = 0x0000;
/**
* File Creation Mode: Allow all other applications to have read access
* To the created file.
* @ See # mode_private
* @ See # mode_world_writeable
*/
Public static final int mode_world_readable = 0x0001;
/**
* File Creation Mode: Allow all other applications to have write access
* To the created file.
* @ See # mode_private
* @ See # mode_world_readable
*/
Public static final int mode_world_writeable = 0x0002;
/**
* File Creation Mode: for use with {@ link # openfileoutput}, if the file
* Already exists then write data to the end of the existing file
* Instead of erasing it.
* @ See # openfileoutput
*/
Public static final int mode_append = 0x8000;
2,
// Create a folder
File destdir = new file ("/data/[your path]/Temp ");
If (! Destdir. exists ()){
Destdir. mkdirs ();
}
Process P;
Int status;
Try {
P = runtime.getruntime(cmd.exe C ("chmod 777" + destdir );
Status = P. waitfor ();
If (status = 0 ){
// Chmod succeed
Toast. maketext (this, "chmod succeed", Toast. length_long). Show ();
} Else {
// Chmod failed
Toast. maketext (this, "chmod failed", Toast. length_long). Show ();
}
}
Reminder:
If it is inserted under the sdcard, it is best to first determine whether the sdcard is inserted, the Code is as follows // first determine whether the sdcard is inserted string status = environment. getexternalstoragestate ();
If (status. Equals (environment. media_mounted )){
Return true;
} Else {
Return false;
} Reference: http://www.devdiv.net/blog/space-28742-do-blog-id-1956.html http://www.phpfans.net/article/htmls/201009/MzAzNjMz.html