簡體   English   中英

在我的應用程序中設置“android.uid.system”后無法訪問sdcard?

[英]It can't access the sdcard after setting “android.uid.system” in my application?

這是一個奇怪的問題。 如果我沒有設置android.uid.system ,我的應用程序可以成功訪問 SD 卡。 但是在將android.uid.system設置為它之后,我的應用程序無法訪問 sdcard。 此時發生異常: 07-13 09:11:24.999: INFO/System.out(9986): create file happen exception--->java.io.IOException: Permission denied 我檢查我在正確的地方寫了正確的權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />. 

因為在我的應用程序中使用forceStopPackage ,所以我需要將android.uid.system添加到清單中。 我已經在 make 文件中寫LOCAL_CERTIFICATE:= platform 誰能解釋這個奇怪的問題。 設置android.uid.system后,我的應用程序屬於系統進程,應該有更多的權力來訪問 sdcard。 這是我的想法。 以下是我的代碼:

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() );
        }

    }

我的應用程序在模擬器上運行,他的目標版本是 2.3。 非常感謝。

請閱讀: link1

這個鏈接2

您可以在 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());
    }
}

這個 function 將被稱為getExternalStorageDirectory() ,因此如果您不破解 aosp,具有系統 uid 的應用程序將無法訪問 sdcard。

使用Environment.getExternalStorageDirectory().getAbsolutePath()獲取路徑,而不是"/mnt/sdcard"

使用: File share = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"test.txt");

這不起作用: File share = new File("/mnt/sdcard", "test.txt");

android.uid.system 使您的應用程序綁定系統,但系統也觀察到sdcard,然后如果您的應用程序刪除它,它可以訪問sdcard。 它似乎

刪除 xml 文件中的這一行:android:sharedUserId="android.uid.system"ystem

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM