How to create a folder and modify its permissions under Android

Source: Internet
Author: User

Original: http://www.cnblogs.com/wanqieddy/archive/2011/12/28/2304906.html

Due to the need for work, today we have studied how to create folders and modify their permissions under Android, and it is necessary to understand that each application package will have a private directory (similar folder) for storing data, and only applications belonging to that package will be able to write to that directory space, and the private data directory bits of each package application In the Android absolute path/data/data/< package name >/directory. In addition to the private Data Catalog application also has the/sdcard directory (that is, the SD card write permissions, but not to modify the file under the SD Card access rights). Other system directories in the file system, and third-party applications are not writable.

The code is as follows two kinds:

1.

Create a folder

File DestDir = new file ("/data/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 the
* Same user ID).
* @see #MODE_WORLD_READABLE
* @see #MODE_WORLD_WRITEABLE
*/
public static final int mode_private = 0x0000;
/**
* File creation Mode:allow All and applications to has 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 and applications to has 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/data/[your path]/temp");
if (!destdir.exists ()) {
Destdir.mkdirs ();
}

Process p;
int status;
try {
p = runtime.getruntime (). EXEC ("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 ();
}
}

Friendly reminder:

If it is inserted under SDcard, it is better to first determine whether the sdcard is inserted, the code is as follows//first to determine whether sdcard Insert string status = Environment.getexternalstoragestate ();
if (Status.equals (environment.media_mounted)) {
return true;
} else {
return false;
}

How to create a folder and modify its permissions under Android

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.