2

I want to update my APK programmatically with a FTP request. I can connect to my FTP server , but i get a E/IO: IOjava.io.FileNotFoundException: /storage/emulated/0/Download/app-debug.apk (Permission denied) exception.

How can i solved this problem? The application is device owner of the device. And i have the permissons in the Android Manifest. Thats why i dont know why i get a permission denied?

 public void run() {
        try {
            FTPClient ftpClient = new FTPClient();
            ftpClient.connect("10.0.2.2");
            ftpClient.login("user", "1234");
            ftpClient.enterLocalPassiveMode();
            String remoteFile1 = "/app-debug.apk";
            String downPath = getExternalStoragePublicDirectory(DIRECTORY_DOWNLOADS).getAbsolutePath();
            File myapk = new File(downPath + "/app-debug.apk");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(myapk));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();
            myapk.setReadable(true, false);
            if (!success) {
                ftpClient.logout();
                ftpClient.disconnect();
                return;
            } else {
                Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                        .setDataAndType(Uri.fromFile(myapk), "application/vnd.android.package-archive")
                        .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(promptInstall);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            Log.e("IO","IO"+e);

        }
  • So your question has nothing to do with [tag:ftp], but only with writing a local file on the Android system, right? – Martin Prikryl Apr 17 '20 at 10:09
  • 2
    **1)** Device Owner can install packages *silently, only on Android 6+*, but you have to use the `PackageInstaller` API. This has been described [in another SO thread](https://stackoverflow.com/questions/32473158). **2)** Writing to external public storage requires a *runtime permission*, manifest entry is not enough. A Device Owner can use `DevicePolicyManager` API to grant the permission. On the other hand, because you would be using `PackageInstaller` API you could download the APK to the app's private cache directory, which is more secure and doesn't require any permissions. – Eugen Pechanec Apr 17 '20 at 10:09
  • As long as you only download a file it has nothing to do with updating. And as said before your problem is only writing a file. And that is because you are on Android Q!? – blackapps Apr 17 '20 at 10:12
  • You have not reacted on three comments. – blackapps Apr 17 '20 at 10:22
  • @MartinPrikryl Yes i want to write a local file on the Android system – user3761485 Apr 17 '20 at 10:26
  • @EugenPechanec How can i download the APK to the app's private cache directory? – user3761485 Apr 17 '20 at 10:30
  • Just as you do now. Only change the path. – blackapps Apr 17 '20 at 11:06
  • 1
    [`Context.getCacheDir()`](https://developer.android.com/reference/android/content/Context#getCacheDir()) – Eugen Pechanec Apr 17 '20 at 11:37
  • 1
    Please add the line - for retaining the binary attribute and to avoid "Package parsing error". ftpClient.setFileType(FTP.BINARY_FILE_TYPE); – Sriram Nadiminti Apr 24 '21 at 06:45

0 Answers0