Cacique
Cacique

Reputation: 31

How to install app on external sdcard programmatically?

I´m currently developing an app which works as an Update Manager. So far the app has work great whenever I installed or updated any other app. All these apps have been installed on the data/app folder and I have used the android.content.pm.IPackageManager class with the installpackage() method to install them.

Im working on a costum ROM which has a Internal Memory (1 GB), a internal SDCARD (4GB), and the External SD CARD (8GB). Due to the fact that all my apps have been installed on the data/app folder I´m running out of space in my internal memory!. I was wondering if i could use the same installpackage() method to install the apps on the external SDCARD (or the internal).

Here is how i install the apps :

 private Class<?> iPackageManagerStubProxyClass;

  iPackageManagerStubProxyClass = Class
 .forName("android.content.pm.IPackageManager$Stub$Proxy");



  public void installPackage(Uri uri) throws NoSuchMethodException,
  NumberFormatException, IllegalArgumentException, IllegalAccessException,
  InvocationTargetException, ClassNotFoundException {
  Log.i(LOG_TAG, "installing without deleting");
  Method method = getMethod(iPackageManagerStubProxyClass, "installPackage");
  method.invoke(packageManagerServiceProxy, uri, getPackageInstallObserver(),
  Integer.valueOf(INSTALL_REPLACE_EXISTING | INSTALL_INTERNAL), "");
  System.out.println(method);
  }

Im starting to suspect that i can´t do the same to install the aplications on the sdcard. Any sugestions? If not ,could I move my apps programatically to the sdcard after instalation?

Upvotes: 3

Views: 5676

Answers (2)

Jorgesys
Jorgesys

Reputation: 126563

How to install app on external sdcard programmatically?

There is no way to do this programatically! (until now Android 7.0)

you must define inside your AndroidManifest.xml file the property android:installLocation

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:installLocation="preferExternal"
    ... >

specifying one of this values:

"internalOnly" The application must be installed on the internal device storage only. If this is set, the application will never be installed on the external storage. If the internal storage is full, then the system will not install the application. This is also the default behavior if you do not define android:installLocation.

"auto" The application may be installed on the external storage, but the system will install the application on the internal storage by default. If the internal storage is full, then the system will install it on the external storage. Once installed, the user can move the application to either internal or external storage through the system settings.

"preferExternal" The application prefers to be installed on the external storage (SD card). There is no guarantee that the system will honor this request. The application might be installed on internal storage if the external media is unavailable or full. Once installed, the user can move the application to either internal or external storage through the system settings.

more information.

Upvotes: 3

Shivputra N
Shivputra N

Reputation: 688

Use this code

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        android:installLocation="preferExternal"
        ... >

Upvotes: 5

Related Questions