[Android][Recovery] Recovery下找不到sdcard路径
程序员文章站
2022-07-06 14:30:39
做升级的时候,把更新包拷贝到sd卡中,然后调用接口进行重启升级 "wossoneri.github.io" 之后进入Recovery模式后报错: 说是找不到 这个路径? 因为上层用Java写路径的时候,获取的是Android的路径,我们知道,adb shell里面是有 的路径的,这个路径实际上并不是 ......
做升级的时候,把更新包拷贝到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