标签: it |
分类:学习 |
写sd card需要在manifest.xml中设置:
在AndroidMenifest.xml中我们可以看到android:sharedUserId="android.uid.system"
但是有了这句后,我就无法对sd卡进行读写操作,比如我丢个test.txt到sd卡下在进行IO操作,是找不到这个文件的
如果我把android:sharedUserId="android.uid.system"注释掉,就可以对test.txt进行IO操作了,也找的到这个文件。
在Settings中android:sharedUserId="android.uid.system"是不可少的,少了它很多Settings下应用直接开不了,或一开就报错。
问下大家,有什么方法在有android:sharedUserId="android.uid.system"的情况下,还能对sd卡进行读写操作吗??
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=63314&page=1#pid636737
//----------------------------------------------------------------------------
对应Android1.5以上的版本中一定要在AndroidManifest.xml中加入:
并且你要确认你的SD卡的读写权限打开的。然后
写入SD卡:File f = newFile(android.os.Environment.getExternalStorageDirectory()+"/test.txt");
String str="this is a test about Write SD card file";
写入方法:
FileOutputStream fileOS=new FileOutputStream(f);
fileOS.write(str.getBytes());
fileOS.close();
BufferedWriter buf = new BufferedWriter (newOutputStreamWriter(fileOS));
buf.write(str,0,str.length());
buf.flush();
buf.close();
//----------------------------------------------------------------------------
SD卡对system用户没有读写的权限,你可以用 ls -l查看一下,如下:
d---rwxr-x system sdcard_rw 2010-07-26 12:56 sdcard 我也在被这个问题困扰着,期待高人能找到方法 |
//----------------------------------------------------------------------------
http://stackoverflow.com/questions/5617797/android-shared-user-id-and-reading-writing-a-file
The system user can not access the SD card, because if the SDcard gets unmounted it may need to kill any processes that havefiles open on it and we don't want system processes being killedlike that. If you want to access the SD card, you need to not usethe system shared user ID.
//----------------------------------------------------------------------------
http://groups.google.com/group/android-porting/browse_thread/thread/c6af65d7ddd5eff3
Processes that continue holding open fds on the sdcard a littleafter it is
requested to be unmounted will be killed so that it canunmount.
We don't want the system process to be able toaccess the sdcard to avoid
these kinds of issues (and just general security cleanliness), sothat it
does not have permission to access it.
Processes that continue holding open fds onthe sdcard a little after it is
requested to be unmounted will be killed so that it canunmount. We don't want the system process to be able to access the sdcard toavoid these kinds of issues (and just general security cleanliness), sothat it does not have permission to access it. 从上面可以看出,system process是不允许操作sdcard的,有个办法是,可以借助一个运行在userprocess中的activity helper类完操作sdcard的动作 |
Processes that continue holding open fds onthe sdcard a little after it is
requested to be unmounted will be killed so that it canunmount. We don't want the system process to be able to access the sdcard toavoid these kinds of issues (and just general security cleanliness), sothat it does not have permission to access it. 从上面可以看出,system process是不允许操作sdcard的,有个办法是,可以借助一个运行在userprocess中的activity helper类完操作sdcard的动作 |
//---------------------------------------------------------------------------
可以改变你的设计模式,我也遇到过这样的问题,我是用app去控制system/bin下面的service,必须要用到system的东西,如果把app的shareuserid设为system的话,那app也就无法读写sdcard,
于是我改变了方法,采用Activity + JNI的方法,在jni里面去使用system,这样的话就会避免sdcard不能被systemprocess读写的问题了.
//----------------------------------------------------------------------------
android:sharedUserId="android.uid.system"是说明你的程序拥有系统级的权限
跟sdcard读写没有任何冲突,之所以会出现楼主的情况,可能是没有sdcard的读写权限
可以用adb shell 进入到/mnt下然后输入ls -l看一下sdcard的权限
//----------------------------------------------------------------------------
问题貌似解决了~
挂载sd卡的时候,把权限设置成775,就可以进行正常的读写操作了~
已经验证通过
//----------------------------------------------------------------------------
请问你是在几点几的平台下操作的?
我的doMount方法比你少个参数,是这样的:
Fat::doMount(devicePath, "/mnt/secure/staging", false, false, 1000,1015, 0702, true)
中间只有2个Boolean值,不知道怎么回事?
//----------------------------------------------------------------------------
在Unix和Linux的各种操作系统下,每个文件(文件夹也被看作是文件)都按读、写、运行设定权限。
读、写、运行三项权限可以用数字表示,就是r=4,w=2,x=1。所以,rw-r--r--用数字表示成644。
反过来说777就是rwxrwxrwx,意思是该登录用户(可以用命令id查看)、他所在的组和其他人都有最高权限。
Android中可用通过adbshell 方法修改文件的权限,有时候我们需要在代码中实现改功能,
try {
Stringcommand = "chmod 777 " + destFile.getAbsolutePath();
Log.i("zyl","command = " + command);
Runtimeruntime = Runtime.getRuntime();
Process proc= runtime.exec(command);
} catch(IOException e) {
Log.i("zyl","chmodfail!!!!");
e.printStackTrace();
}
[html]
对于android的sdcard不能直接通过chmod命令来修改对于system组的读写权限。但是可以通过修改源码方式实现,其实也很简单,方法如下:
android2.2以后修改
修改/system/core/vold/Volume.cpp 文件
将
[html]
Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
1000, 1015, 0702, true))
修改为:
[html]
Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false,
1000, 1015, 0002, true))
参数含义:
1000代表的是uid,即sytem
1015代表的是gid,
0002:是对权限的掩码
0002:表示 system: rwx-rwxr-x,此时system有读写执行权限
如果system不需要写权限可以将0002改为
0202: 表示 r-xrwxr-x。
默认的0702代表 ---rwxr-x
。
然后将vold重新编译一下,用adb push 命令push到 /system/bin/目录下。重新启动一下机器就OK了。
对于android 2.1以下:
可以
修改/system/core/vold/volmgr_vfat.c
[html]
rc = mount(devpath, vol->mount_point, "vfat", flags,"utf8,uid=1000,gid=1000,fmask=711,dmask=700,shortname=mixed");
改为
[html]
rc = mount(devpath, vol->mount_point, "vfat", flags,"utf8,uid=1000,gid=1000,fmask=0,dmask=0,shortname=mixed");
这里的dmask和fmask和上面的作用类似。通过改写711和700可以配置出不同的权限。