如何安装存储在Android应用程序的私人存储中的apk?
在我的 Android 应用程序中,我从网络下载一个 apk,将其存储在应用程序的私有存储中 (openFileOutput(FILENAME, Context.MODE_PRIVATE))
并尝试为该下载的 apk 调用 android 软件包安装程序通过,intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
startActivity(intent);
但我收到一条错误消息无法打开 zip:/data/data/com.test/files/abc.apk:权限被拒绝
在 LogCat 中
,解析错误:解析包时出现问题
在手机屏幕上。
发生这种情况是因为 apk 文件位于应用程序的私有存储中,所以 Android 软件包安装程序无法访问它吗?如果是,是否仍然可以安装该 apk(当然需要用户的许可。)或者我做错了什么?
PS:请不要假设手机已root。
From my android app, I'm downloading an apk from the web, storing it in application's private storage (openFileOutput(FILENAME, Context.MODE_PRIVATE))
and trying to call the android package installer for this downloaded apk by,intent.setDataAndType(Uri.fromFile(f), "application/vnd.android.package-archive");
startActivity(intent);
but I'm getting an error which saysUnable to open zip: /data/data/com.test/files/abc.apk : Permission denied
in the LogCat
and,Parse Error: There is a problem parsing the package
on the phone screen.
Is it happening because the apk file is in application's private storage so the Android package installer can't access it? If yes, is it somehow possible to still get the apk installed (with user's permission of course.) or Am I doing something wrong?
PS: Please don't assume the phone to be rooted.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了 Mark Allisons 的评论之外,我的猜测是,由于您已使用 mode_private 将文件保存到私有存储中,因此只有您的应用程序具有读/写权限。但是您试图让软件包安装程序读取它,这意味着该文件必须由外部应用程序访问,因此应该是 MODE_WORLD_READABLE 或 MODE_WORLD_WRITEABLE。
或者您可以将其保存到默认情况下全球可读的外部存储中。
In addition to Mark Allisons comment, my GUESS is that since you have the file saved to private storage with mode_private, only your application has read/write permission. But you are trying to have the package installer read it which means that the file must be accessed by an External application and therefore should either be MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE.
Or you could save it to external storage where it is world readable by default.
好的,现在我已经找到了一个解决方法(这也帮助我确认了确切的问题 - 问题是安装程序无法访问 apk 文件)。现在,我在将文件保存在内部存储中时使用 MODE_WORLD_READABLE 而不是 MODE_PRIVATE,并且 Android 安装程序能够访问 apk 并安装它而不会出现任何错误。
实际上,对我来说,使用内部存储的主要目的是用户不应该能够直接复制 apk 文件(假设一个简单的威胁模型,其中用户没有 root 手机,但可以通过 SD 卡浏览以获取找到 apk 并复制它)。尽管我仍然不确定该文件现在对用户是否可见?我的意思是,如果下载的 apk 可以从手机中的应用程序访问,但用户不能直接复制,那么我(几乎)没问题。
如果知道 MODE_WORLD_READABLE 确切范围的人可以详细说明这一点,特别是是否可以在(未 root 的)手机中浏览以此模式保存的文件,将会很有帮助。另外,是否有可能有更好的策略来保护 apk,同时仍然允许安装程序访问它?
Ok, for now I've figured out a workaround (and which helped me confirming the exact problem also- the problem was that installer couldn't access the apk file). Now, I'm using MODE_WORLD_READABLE instead of MODE_PRIVATE while saving the file in internal storage, and the android installer is able to access the apk and can install it without any error.
Actually, for me the main purpose of using internal storage was that users shouldn't be able to copy the apk file directly (assuming a simple threat model in which a user doesn't have rooted phone, but can browse through the SD card to find the apk and copy it). Though I'm still not sure whether the file is visible to a user now or not? I mean I'm (almost) fine if the downloaded apk can be accessed from an app in the phone, but can't be copied directly by the user.
Would be helpful if someone knowing the exact scope of MODE_WORLD_READABLE could elaborate on the same, specifically whether a file saved in this mode can be browsed in the (unrooted) phone. Also, is it possible to have a better strategy to safeguard the apk while still allowing the installer to access it?
MODE_WORLD_READABLE 确实表示它可以被整个“手机世界”访问,因此其他应用程序可以获取它,包括文件浏览器。某些程序(例如 es-file manager)可以让您查看设备私有存储上的内容,即使您没有 root,但不允许您更改这些文件。为什么安装后不立即删除 apk 文件?另外,由于您在应用程序中下载 apk 文件,我猜您不想使用 Play 商店,但如果这不是问题,请参阅应用程序的许可选项:http://developer.android.com/google/play/licensing/overview.html 因为可能有涵盖您的用例要求。
MODE_WORLD_READABLE does say that it can be accessed by whole "world on the phone" so other apps can get it including file explorers. Some programs like es-file manager let you see contents on device private storage even if you are not rooted but don't let you change those files. Why don't you just delete apk file immediately after installation ? Also since you are downloading apk file in your application I am guessing you don't want to use play store but if that is not an issue see Licensing options for your app on: http://developer.android.com/google/play/licensing/overview.html since there is probably use case that covers your requirements.