在android中设置文件读取权限
我正在编写一个应用程序,它从网址下载 *.apk,然后尝试安装它。 当我尝试让 PackageManager 安装它时,我似乎遇到了权限被拒绝的错误。 我想将文件的权限设置为可从 java 代码读取。你怎么做到这一点。
这是我阅读文件的方式
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(PATH + fileName);
byte data[] = new byte[1024];
int count;
while((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
I am writing an app which downloads an *.apk from a url and then tries to install it.
It appears I am running into Permission denied error when trying to get PackageManager to install it.
I want set the permission of the file to readable from java code. How do you do this.
Here is how I am reading the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(PATH + fileName);
byte data[] = new byte[1024];
int count;
while((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法直接使用
PackageManager
安装 .apk 文件。只有系统应用程序可以做到这一点。但您可以要求系统使用标准安装工作流程安装应用程序。这是一个示例:
这一切都假设您实际上成功下载了
.apk
。但如果您在这一步失败,您应该检查您的 WRITE_EXTERNAL_STORAGE允许。并且还要检查您的 SD 卡是否未通过 USB 共享(如果是这样,您的应用程序将没有对 SD 卡的写入权限)。You can't install .apk files directly using
PackageManager
. Only system applications can do this.But you can ask system to install application using standard installation workflow. Here is an example:
This all assuming you actually succeeded downloading the
.apk
. But if you failed at that step, you should check your WRITE_EXTERNAL_STORAGE permission. And also check that your sdcard is not shared via USB (if so, your application will not have write permission to sdcard).如果您想更改 android 2.2 及更高版本上的文件权限,您可以使用以下方法:
在 android 3 中,您也可以在不使用本机代码解决方法的情况下执行此操作:
if you want to change file permissions on android 2.2 and higher you can use this:
in android 3 you can do it also without using a native code workaround:
我还没有尝试过这里所要求的内容,但这会改变任何版本的 android 中的文件权限:
这就像 Shmuel 所建议的那样,但更完整,顺便说一句,他的建议确实适用于 1.5 及更高版本,而不是2.2 及更高版本。
I haven't tried exactly what is being asked here, but this will change a files permissions in any version of android:
This is like what Shmuel was suggesting, but more complete and btw, what he suggested does work in 1.5 and higher, not 2.2 and higher.