系统资源resources中的文件的无法访问
将应用通过AndroidManifest文件配置android:sharedUserId="android.uid.system"并使用系统签名升级为系统应用后无法加载资源下的文件
加载方式:ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
classLoader.getResourceAsStream("xxx.xml");
classLoader.getResourceAsStream("/xxx.xml");
classLoader.getResourceAsStream("META-INF/xxx.xml");
classLoader.getResourceAsStream("/META-INF/xxx.xml");
等方式都尝试了 提示文件找不到
仅供参考:
使用 android:sharedUserId="android.uid.system" 将应用配置为系统应用后,该应用将与系统应用运行在相同的进程中,并共享相同的 uid,具体来说,将应用配置为系统应用后,该应用将使用系统应用的 UID 运行,其资源访问权限也将与系统应用相同。因此,在该模式下,系统应用将可以通过 Resources 访问该应用的资源,但是该应用将无法直接访问系统应用的资源。
为了使您的应用能够使用系统应用的权限访问资源,您可以使用 PackageManager 来获取系统应用的资源,并将其复制到您的应用的内部存储中,然后再使用 Context 加载该资源。具体来说,您可以使用以下代码来将系统应用的资源复制到您的应用的内部存储中:
try {
Context sysContext = createPackageContext("system_package_name", Context.CONTEXT_IGNORE_SECURITY);
Resources res = sysContext.getResources();
InputStream is = res.openRawResource(sysContext.getResources().getIdentifier("resource_name", "resource_type", "system_package_name"));
FileOutputStream fos = openFileOutput("resource_name", Context.MODE_PRIVATE);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
}
fos.close();
is.close();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
然后,您可以使用 Context 加载该资源:
InputStream is = openFileInput("resource_name");
这样,您的应用就可以使用系统应用的资源了。请注意,这种方式可能会使您的应用占用更多的存储空间,并且需要在每次启动应用时执行复制操作。因此,建议您在必要的时候使用此方式。