Create and change folder permissions in Android-Java code

Source: Internet
Author: User

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 bit of each package Application
In the absolute path/data/<package name>/directory of Android. In addition to the private data directory, the app also has the/sdcard directory (that is, SD
The write permission of the 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
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
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.