Umesh Umesh - 10 days ago 5
Android Question

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

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 through code, without user having to explicitly install it. The catch is that i cannot use an SD card download the apk file.

I can navigate to the data/data/files folder and can see my file downloaded. The only problem is that i cannot get it installed. This is what i get

'/data/data/org.obs.testinstall.main/files/app.apk': Permission denied


I understand that Android does not give permission to access the data directory.
My question is how can i download and install an application(apk) without using a SD card. This application is not intended to be published in the market. I have tried using both the Internal Storage using

FileOutputStream fos = openFileOutput("app.apk", Context.MODE_PRIVATE);


and the cache directory

File file = getCacheDir();
File outputFile = new File(file, "app.apk");


Both give the same result .. "Permission denied"

When i change the code to incorporate an SD card the application works perfectly, but using an SD card is not an option.

Surely there must be a way to do this. It is hard to believe that such a handicap exist in the Android O/S.

Has anybody done this? Any workarounds? Any pointers would be helpful.

Answer

It it caused by android application can not read from another application file if it is written using PRIVATE mode.

You can do this:

String fileName = "tmp.apk";
FileOutputStream fos = openFileOutput(fileName,
        MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE);

// write the .apk content here ... flush() and close()

// Now start the standard instalation window
File fileLocation = new File(context.getFilesDir(), fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(fileLocation),
                       "application/vnd.android.package-archive");
context.startActivity(intent);

Be careful though, because that file is now world-visible, and can be seen by any application in the same device, if they know the file location.