[英]It can't access the sdcard after setting “android.uid.system” in my application?
This is a strange problem.这是一个奇怪的问题。 My Application can access the sdcard successfully if I don't set
android.uid.system
to it.如果我没有设置
android.uid.system
,我的应用程序可以成功访问 SD 卡。 But after setting android.uid.system
to it, my application can't access the sdcard.但是在将
android.uid.system
设置为它之后,我的应用程序无法访问 sdcard。 At this time, the exception take place: 07-13 09:11:24.999: INFO/System.out(9986): create file happen exception--->java.io.IOException: Permission denied
.此时发生异常:
07-13 09:11:24.999: INFO/System.out(9986): create file happen exception--->java.io.IOException: Permission denied
。 I check I write the right permission in the right place:我检查我在正确的地方写了正确的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />.
Because use forceStopPackage
in my application, I need to add android.uid.system
to the manifest.因为在我的应用程序中使用
forceStopPackage
,所以我需要将android.uid.system
添加到清单中。 And I have written LOCAL_CERTIFICATE:= platform
in the make file.我已经在 make 文件中写
LOCAL_CERTIFICATE:= platform
。 Who can explain this strange problem.谁能解释这个奇怪的问题。 After setting
android.uid.system
, my application is belong the system process which should have more power to access the sdcard.设置
android.uid.system
后,我的应用程序属于系统进程,应该有更多的权力来访问 sdcard。 This is my idea.这是我的想法。 The following are my code:
以下是我的代码:
public void setPackage(String dir){
System.out.println( "setPackage dir=="+dir );
File share=new File("/mnt/sdcard","test.txt");
if(!share.exists()){
try{
share.createNewFile();
}catch(Exception e){
System.out.println( "creat file happen exception--->" +e.toString() );
}
}
try{
if(share!=null){
System.out.println( "create file is not null" );
FileOutputStream fops=new FileOutputStream(share);
fops.write(dir.getBytes());
fops.flush();
fops.close();
}
}catch(Exception e){
System.out.println( "write Exception-->" +e.toString() );
}
}
And My application run at the emulator and his target version is 2.3.我的应用程序在模拟器上运行,他的目标版本是 2.3。 Thank you very much.
非常感谢。
You can see in android source code: frameworks\base\core\java\android\os\Environment.java, it have a function:您可以在 android 源代码中看到:frameworks\base\core\java\android\os\Environment.java,它有一个 function:
private static void throwIfSystem() {
if (Process.myUid() == Process.SYSTEM_UID) {
Log.wtf(TAG, "Static storage paths aren't available from AID_SYSTEM", new Throwable());
}
}
This function would be called getExternalStorageDirectory()
, hence app with system uid can't access sdcard if you don't hack aosp.这个 function 将被称为
getExternalStorageDirectory()
,因此如果您不破解 aosp,具有系统 uid 的应用程序将无法访问 sdcard。
Use Environment.getExternalStorageDirectory().getAbsolutePath()
to get the path, NOT "/mnt/sdcard"
使用
Environment.getExternalStorageDirectory().getAbsolutePath()
获取路径,而不是"/mnt/sdcard"
Use: File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");
使用:
File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");
This won't work: File share = new File("/mnt/sdcard", "test.txt");
这不起作用:
File share = new File("/mnt/sdcard", "test.txt");
android.uid.system make your app bind system, but sdcard observered by system also, then if your app delete this, it can access the sdcard. android.uid.system 使您的应用程序绑定系统,但系统也观察到sdcard,然后如果您的应用程序删除它,它可以访问sdcard。 it seems
它似乎
delete this line in your xml file:android:sharedUserId="android.uid.system"ystem删除 xml 文件中的这一行:android:sharedUserId="android.uid.system"ystem
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.