Kirk
Kirk

Reputation: 16265

Filter list of apps on SD card / internal

Update I must target Android 2.1.

I am trying to find out how to list apps programatically, filtered by being on the SD card or internal storage.

I'm not certain how to even approach this. Should I first detect the SD card mount point and see if an app data directory is on it? For example

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
  Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);     
  List<ResolveInfo> intents = m_pm.queryIntentActivities(mainIntent, 0);
  File baseSD = Environment.getExternalStorageDirectory();

  int count = intents.size();
  for (int i = 0; i < count; i++) {
    String baseApp = intents.get(i).activityInfo.applicationInfo.dataDir;
    if (baseApp.startsWith(baseSD.getAbsolutePath()) {
      // This is on the SD card, do whatever with the app details
    }
    else {
      // This is on internal storage
    }
  }
}

Seems very low-brow, there must be a more proper way. Any help is appreciated.

Upvotes: 2

Views: 761

Answers (1)

gwvatieri
gwvatieri

Reputation: 5183

You can use this code for checking where an app is installed:

PackageInfo pi = pm.getPackageInfo("packageName", 0);

if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == 0)
   ; // stored on internal storage
else 
   ; // stored on sd

Be careful because FLAG_EXTERNAL_STORAGE is available starting from Android API 8.

Upvotes: 3

Related Questions