Search code examples
androidcachingsaveapkinternal

Install .APK from Android Cache


I have problems installing an APK saved in Android internal Cache. There are no issues saving the file in External Storage or on External Cache using context.getExternalCacheDir().

But if I try to use context.getCacheDir(), the log returns

/data/data/com.my.package/cache/update.apk: open failed: EACCES (Permission denied)

                File file = context.getCacheDir();

                File outputFile = new File(file, "update.apk");
                if(outputFile.exists()){
                    outputFile.delete();
                }

                FileOutputStream fos = new FileOutputStream(outputFile);


                InputStream is = c.getInputStream();

                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ((len1 = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len1);
                }
                fos.close();
                is.close();

                Intent intent = new Intent(Intent.ACTION_VIEW);

                //SAVE IN CACHE
                intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
                context.startActivity(intent);

It looks like internal cache doesn't allow the APK to be correctly read.

The fact is, if the file is saved on the External Storage or external Cache the APK will be available to the user, and I don't want that.

What can it be don to save the file in internal Cache?

Thanks


Solution

  • It looks like internal cache doesn't allow the APK to be correctly read.

    That is because the installer app has no rights to read your file.

    The fact is, if the file is saved on the External Storage or external Cache the APK will be available to the user, and I don't want that.

    Then do not install it on the user's device. Any user can copy any APK off their device at any time after installation, so the only way to prevent the user from accessing the APK is to not have it on the device in the first place.

    What can it be don to save the file in internal Cache?

    Probably nothing. If you switch to openFileOutput(), you can see if MODE_WORLD_READABLE will be sufficient for the installer to proceed. Again, this will not stop the user from being able to access the APK file.