在android中设置文件读取权限

发布于 2024-11-11 16:41:36 字数 473 浏览 2 评论 0原文

我正在编写一个应用程序,它从网址下载 *.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

远山浅 2024-11-18 16:41:36

您无法直接使用 PackageManager 安装 .apk 文件。只有系统应用程序可以做到这一点。

但您可以要求系统使用标准安装工作流程安装应用程序。这是一个示例:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(pathToApk));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);

这一切都假设您实际上成功下载了 .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:

Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(pathToApk));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);

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).

攒眉千度 2024-11-18 16:41:36

如果您想更改 android 2.2 及更高版本上的文件权限,您可以使用以下方法:

Runtime.getRuntime().exec("chmod 777 " + PATH + fileName);

在 android 3 中,您也可以在不使用本机代码解决方法的情况下执行此操作:

new java.io.File(PATH + fileName).setReadable(true, false);

if you want to change file permissions on android 2.2 and higher you can use this:

Runtime.getRuntime().exec("chmod 777 " + PATH + fileName);

in android 3 you can do it also without using a native code workaround:

new java.io.File(PATH + fileName).setReadable(true, false);
跨年 2024-11-18 16:41:36

我还没有尝试过这里所要求的内容,但这会改变任何版本的 android 中的文件权限:

exec("chmod 0777 " + fileOfInterest.getAbsolutePath());  //whatever permissions you require

private void exec(String command) {
    Runtime runtime = Runtime.getRuntime();
    Process process;
    try {
        process = runtime.exec(command);
        try {
            String str;
            process.waitFor();
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((str = stdError.readLine()) != null) {
                Log.e("Exec",str);
                mErrOcc = true;  //I use this elsewhere to determine if an error was encountered
            }
            process.getInputStream().close();
            process.getOutputStream().close();
            process.getErrorStream().close();
        } catch (InterruptedException e) {
            mErrOcc = true;
        }
    } catch (IOException e1) {
        mErrOcc = true;
    }
}

这就像 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:

exec("chmod 0777 " + fileOfInterest.getAbsolutePath());  //whatever permissions you require

private void exec(String command) {
    Runtime runtime = Runtime.getRuntime();
    Process process;
    try {
        process = runtime.exec(command);
        try {
            String str;
            process.waitFor();
            BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
            while ((str = stdError.readLine()) != null) {
                Log.e("Exec",str);
                mErrOcc = true;  //I use this elsewhere to determine if an error was encountered
            }
            process.getInputStream().close();
            process.getOutputStream().close();
            process.getErrorStream().close();
        } catch (InterruptedException e) {
            mErrOcc = true;
        }
    } catch (IOException e1) {
        mErrOcc = true;
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文