is Installed On Sd Card - Android Hardware

Android examples for Hardware:SD Card

Description

is Installed On Sd Card

Demo Code


//package com.java2s;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;

public class Main {
    public static boolean isInstalledOnSdCard(Context context) {
        // check for API level 8 and higher
        if (isCompatible(8)) {
            PackageManager pm = context.getPackageManager();
            try {
                PackageInfo pi = pm.getPackageInfo(
                        context.getPackageName(), 0);
                ApplicationInfo ai = pi.applicationInfo;
                return (ai.flags & 0x00040000 /*
                                               * ApplicationInfo.
                                               * FLAG_EXTERNAL_STORAGE
                                               */) == 0x00040000 /*
                                                                 * ApplicationInfo.
                                                                 * FLAG_EXTERNAL_STORAGE
                                                                 */;
            } catch (NameNotFoundException e) {
                // ignore
            }/*  w  w  w. ja  v  a2  s  .c  o  m*/
        }

        // check for API level 7 - check files dir
        try {
            String filesDir = context.getFilesDir().getAbsolutePath();
            if (filesDir.startsWith("/data/")) {
                return false;
            } else if (filesDir.contains(Environment
                    .getExternalStorageDirectory().getPath())) {
                return true;
            }
        } catch (Throwable e) {
            // ignore
        }

        return false;
    }

    public static boolean isCompatible(int apiLevel) {
        return android.os.Build.VERSION.SDK_INT >= apiLevel;
    }
}

Related Tutorials