开发者

set file read permission to file in android

开发者 https://www.devze.com 2023-03-10 02:06 出处:网络
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 i

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);
}


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号