Download and install an application (apk) from internal memory - no SD card

后端 未结 6 1936
庸人自扰
庸人自扰 2021-01-06 19:35

Greetings and a happy new year to all my fellow programmers.

My code downloads an apk file from a remote server. I need to initiate the installation procedure throug

6条回答
  •  情深已故
    2021-01-06 20:24

    when you send intent to install apk, you can use this function to change mode for apk directory.

    private static boolean changeMode(String filePath, String prefixPath) {
        if (TextUtils.isEmpty(prefixPath) || !filePath.startsWith(prefixPath)) {
            return true;
        }
    
        try {
            String[] args1 = { "chmod", "705", prefixPath};
            Runtime.getRuntime().exec(args1);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    
        String subPath = filePath.split(prefixPath)[1];
        String[] subArr = subPath.split(File.separator);
        for (String path : subArr) {
            if (!TextUtils.isEmpty(path)) {
                prefixPath = prefixPath + File.separator + path;
                try {
                    if (!prefixPath.endsWith(".apk")) {
                        String[] progArray1 = {"chmod", "705", prefixPath};
                        Runtime.getRuntime().exec(progArray1);
                    } else {
                        String[] progArray2 = {"chmod", "604", prefixPath};
                        Runtime.getRuntime().exec(progArray2);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }
    

    And before you send intent, check chmod is it alreay success.

        boolean chmodRes = changeMode(filePath, context.getCacheDir().getAbsolutePath())
                && changeMode(filePath, context.getFilesDir().getAbsolutePath());
        if (!chmodRes) {
            return false;
        }
    

提交回复
热议问题