Recovery文件路徑

[Android][Recovery] Recovery下找不到sdcard路徑

做升級的時候,把更新包拷貝到sd卡中,然後調用接口進行重啓升級

wossoneri.github.io
File update_file = new File("/sdcard/update.zip");
try {
Log.d(“WOW”, "install " + update_file.getAbsolutePath());
RecoverySystem.installPackage(getBaseContext(), update_file);
} catch (IOException e) {
e.printStackTrace();
}

之後進入Recovery模式後報錯:
Supported API: 3
charge_status 3, charged 0, status 0, capacity 62
Finding update package…
Opening update package…
E:unknow volume for path [/storage/emulated/0/update.zip]
E:failed to map file
Installation aborted.

說是找不到/storage/emulated/0這個路徑?
因爲上層用Java寫路徑的時候,獲取的是Android的路徑,我們知道,adb shell裏面是有/sdcard的路徑的,這個路徑實際上並不是插入的SD卡路徑,而是一個內置路徑。

內置路徑通過 ls -l 可以看到 /sdcard 的映射

lrwxrwxrwx 1 root root 21 1970-01-01 08:00 sdcard -> /storage/self/primary

也就是說下面幾個路徑是一樣的

/sdcard/

/storage/emulated/0

/storage/self/primary
而外置sd卡路徑是

/storage/0658-0900

所以,我們代碼裏寫的是/sdcard但是傳到Recovery的路徑就變成/storage/emulated/0了。
我們的需求是把升級包放到sdcard裏面去,所以就需要修改Recovery裏的文件路徑。
實際要做的就是把獲得到的路徑裏面/storage/emulated/0替換成/sdcard即可:

Recovery裏面的sd卡路徑就是/sdcard/

if (update_package) {
    // For backwards compatibility on the cache partition only, if
    // we're given an old 'root' path "CACHE:foo", change it to
    // "/cache/foo".
    if (strncmp(update_package, "CACHE:", 6) == 0) {
        int len = strlen(update_package) + 10;
        char* modified_path = (char*)malloc(len);
        if (modified_path) {
            strlcpy(modified_path, "/cache/", len);
            strlcat(modified_path, update_package+6, len);
            printf("(replacing path \"%s\" with \"%s\")\n",
                   update_package, modified_path);
            update_package = modified_path;
        }
        else
            printf("modified_path allocation failed\n");
    } else if(strncmp(update_package, "/storage/emulated/0/", 20) == 0) {
        int len = strlen(update_package) + 20;
        char* modified_path = (char*)malloc(len);
        if (modified_path) {
            strlcpy(modified_path, "/sdcard/", len);
            strlcat(modified_path, update_package+20, len);
            printf("(replacing path \"%s\" with \"%s\")\n",
                   update_package, modified_path);
            update_package = modified_path;
        }
        else
            printf("modified_path allocation failed\n");
    }

Ref https://blog.csdn.net/wed110/article/details/9943915?utm_source=blogxgwz1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章