android下创建文件夹和修改其权限的方法
2011-12-28 15:51
435 查看
由于工作的需要,今天研究了在android下创建文件夹和修改其权限的方法,需要了解的是每个应用程序包都会有一个私有的存储数据的目录(类似文件夹),只有属于该包的应用程序才能写入该目录空间,每个包应用程序的私有数据目录位 于Android绝对路径/data/data/<包名>/目录中。除了私有数据目录应用程序还拥有/sdcard目录(即SD Card的写入权限,但不可以修改sd card下文件的访问权限)。文件系统中其他系统目录,第三方应用程序是不可写入的。
代码如下两种:
1、
//创建文件夹
File destDir = new File(“/data/data/[your path]/temp”);
if (!destDir.exists()) {
destDir.mkdirs();
}
//修改权限
FileOutputStream fos;
fos = openFileOutput("filename" , MODE_WORLD_READABLE);
备注:可用的mode 参数如下:
/**
* 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 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、
//创建文件夹
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();
}
}
友情提醒:
如果是在sdcard下插入,最好先判断sdcard是否插入,代码如下
//首先判断sdcard是否插入
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
参考:http://www.devdiv.net/blog/space-28742-do-blog-id-1956.html http://www.phpfans.net/article/htmls/201009/MzAzNjMz.html
代码如下两种:
1、
//创建文件夹
File destDir = new File(“/data/data/[your path]/temp”);
if (!destDir.exists()) {
destDir.mkdirs();
}
//修改权限
FileOutputStream fos;
fos = openFileOutput("filename" , MODE_WORLD_READABLE);
备注:可用的mode 参数如下:
/**
* 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 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、
//创建文件夹
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();
}
}
友情提醒:
如果是在sdcard下插入,最好先判断sdcard是否插入,代码如下
//首先判断sdcard是否插入
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return true;
} else {
return false;
}
参考:http://www.devdiv.net/blog/space-28742-do-blog-id-1956.html http://www.phpfans.net/article/htmls/201009/MzAzNjMz.html
相关文章推荐
- 在android下创建文件夹及修改其权限的方法
- android下创建文件夹和修改其权限的方法
- 文件夹权限的修改方法
- Android实时文件夹创建方法
- ubuntu修改文件夹或文件权限方法
- Android修改文件和文件夹权限
- 在NETAPP CIFS文件夹中强制修改权限的方法 推荐
- android创建和删除文件夹和文件的实现方法
- 【Android存储权限问题】AS开发的应用,manifest配置了读写SD卡权限,安装却无法创建文件夹
- mac home目录创建文件夹,修改权限
- Mac 创建和打开含有空格文件的文件夹解决方案;使用修改root密码,使用root 权限
- Linux_修改创建文件夹时默认权限(修改为能上传)
- Linux chmod命令修改文件与文件夹权限方法
- Android中修改设备权限的方法
- day05 创建用户过程、文件夹,文件等权限修改等
- Linux系统下的文件和文件夹相关操作(创建/删除/修改权限)
- Android中在sdcard上创建文件夹,注意加权限!!!!
- ubuntu下无法在根目录创建文件夹;permission denied 权限不足问题解决方法
- Linux chmod命令修改文件与文件夹权限方法