问题描述
向所有程序员同事问好,新年快乐.
Greetings and a happy new year to all my fellow programmers.
我的代码从远程服务器下载了一个 apk 文件.我需要通过代码启动安装过程,而无需用户明确安装.问题是我不能使用 SD 卡下载 apk 文件.
My code downloads an apk file from a remote server. I need to initiate the installation procedure through code, without user having to explicitly install it. The catch is that i cannot use an SD card download the apk file.
我可以导航到 data/data/files 文件夹并可以看到我下载的文件.唯一的问题是我无法安装它.这是我得到的
I can navigate to the data/data/files folder and can see my file downloaded. The only problem is that i cannot get it installed. This is what i get
'/data/data/org.obs.testinstall.main/files/app.apk': Permission denied
我了解 Android 没有授予访问数据目录的权限.我的问题是如何在不使用 SD 卡的情况下下载和安装应用程序(apk).此应用程序不打算在市场上发布.我已经尝试使用
I understand that Android does not give permission to access the data directory.My question is how can i download and install an application(apk) without using a SD card. This application is not intended to be published in the market. I have tried using both the Internal Storage using
FileOutputStream fos = openFileOutput("app.apk", Context.MODE_PRIVATE);
和缓存目录
File file = getCacheDir();
File outputFile = new File(file, "app.apk");
两者都给出相同的结果..权限被拒绝"
Both give the same result .. "Permission denied"
当我更改代码以合并 SD 卡时,应用程序运行良好,但不能选择使用 SD 卡.
When i change the code to incorporate an SD card the application works perfectly, but using an SD card is not an option.
当然必须有办法做到这一点.很难相信 Android O/S 中存在这样的障碍.
Surely there must be a way to do this. It is hard to believe that such a handicap exist in the Android O/S.
有人做过吗?任何解决方法?任何指针都会有所帮助.
Has anybody done this? Any workarounds? Any pointers would be helpful.
推荐答案
android应用程序无法读取导致另一个应用程序文件,如果它是使用 PRIVATE 模式编写的.
It it caused by android application can not read fromanother application file if it is written using PRIVATE mode.
你可以这样做:
String fileName = "tmp.apk";
FileOutputStream fos = openFileOutput(fileName,
MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);
// write the .apk content here ... flush() and close()
// Now start the standard instalation window
File fileLocation = new File(context.getFilesDir(), fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(fileLocation),
"application/vnd.android.package-archive");
context.startActivity(intent);
不过要小心,因为该文件现在是世界可见的,并且可以被同一设备中的任何应用程序看到,如果他们知道文件位置.
Be careful though, because that file is now world-visible,and can be seen by any application in the same device,if they know the file location.
这篇关于从内存下载并安装应用程序 (apk) - 无 SD 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!