Example usage for android.content Context getFilesDir

List of usage examples for android.content Context getFilesDir

Introduction

In this page you can find the example usage for android.content Context getFilesDir.

Prototype

public abstract File getFilesDir();

Source Link

Document

Returns the absolute path to the directory on the filesystem where files created with #openFileOutput are stored.

Usage

From source file:com.nullwire.ExceptionHandler.java

/**
 * Register handler for unhandled exceptions.
 * @param context//from   ww w .ja v a  2  s . com
 */
public static boolean register(Context context) {
    Log.i(TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        G.APP_VERSION = pi.versionName;
        // Package name
        G.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        G.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        G.PHONE_MODEL = android.os.Build.MODEL;
        // Android version
        G.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Log.d(TAG, "TRACE_VERSION: " + G.TraceVersion);
    Log.d(TAG, "APP_VERSION: " + G.APP_VERSION);
    Log.d(TAG, "APP_PACKAGE: " + G.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + G.FILES_PATH);
    Log.d(TAG, "URL: " + G.URL);

    boolean stackTracesFound = false;
    // We'll return true if any stack traces were found
    if (searchForStackTraces().length > 0) {
        stackTracesFound = true;
    }

    new Thread() {
        @Override
        public void run() {
            // First of all transmit any stack traces that may be lying around
            submitStackTraces();
            UncaughtExceptionHandler currentHandler = Thread.getDefaultUncaughtExceptionHandler();
            if (currentHandler != null) {
                Log.d(TAG, "current handler class=" + currentHandler.getClass().getName());
            }
            // don't register again if already registered
            if (!(currentHandler instanceof DefaultExceptionHandler)) {
                // Register default exceptions handler
                Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(currentHandler));
            }
        }
    }.start();

    return stackTracesFound;
}

From source file:demo.vicshady.classes.ErrReport.ExceptionHandler.java

/**
 * Register handler for unhandled exceptions.
 * @param context//w w w .  j  av  a 2 s  .com
 */
public static boolean register(Context context) {
    Log.i(TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        G.APP_VERSION = pi.versionName;
        // Package name
        G.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        G.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        G.PHONE_MODEL = android.os.Build.MODEL;
        // Android version
        G.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Log.i(TAG, "TRACE_VERSION: " + G.TraceVersion);
    Log.d(TAG, "APP_VERSION: " + G.APP_VERSION);
    Log.d(TAG, "APP_PACKAGE: " + G.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + G.FILES_PATH);
    Log.d(TAG, "URL: " + G.URL);

    boolean stackTracesFound = false;
    // We'll return true if any stack traces were found
    if (searchForStackTraces().length > 0) {
        stackTracesFound = true;
    }

    new Thread() {
        @Override
        public void run() {
            // First of all transmit any stack traces that may be lying around
            submitStackTraces();
            UncaughtExceptionHandler currentHandler = Thread.getDefaultUncaughtExceptionHandler();
            if (currentHandler != null) {
                Log.d(TAG, "current handler class=" + currentHandler.getClass().getName());
            }
            // don't register again if already registered
            if (!(currentHandler instanceof DefaultExceptionHandler)) {
                // Register default exceptions handler
                Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(currentHandler));
            }
        }
    }.start();

    return stackTracesFound;
}

From source file:Main.java

/**
 * Checks if the application is installed on the SD card. See
 * http://stackoverflow.com/questions//* w  w  w  .  j a  v  a 2 s  .  com*/
 * 5814474/how-can-i-find-out-if-my-app-is-installed-on-sd-card
 *
 * @return <code>true</code> if the application is installed on the sd card
 */
@SuppressLint("SdCardPath")
public static boolean isInstalledOnSdCard(Context context) {
    // check for API level 8 and higher
    if (VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            ApplicationInfo ai = pi.applicationInfo;
            return (ai.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE;
        } catch (NameNotFoundException e) {
            // ignore
        }
    }

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

    return false;
}

From source file:org.goodev.droidddle.utils.IOUtil.java

public static File getBestAvailableFilesRoot(Context context) {
    File[] roots = ContextCompat.getExternalFilesDirs(context, null);
    if (roots != null) {
        for (File root : roots) {
            if (root == null) {
                continue;
            }/*from   w  w w  .  ja  v  a 2 s. c o m*/

            if (Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(root))) {
                return root;
            }
        }
    }

    // Worst case, resort to internal storage
    return context.getFilesDir();
}

From source file:com.android.settings.util.Helpers2.java

public static String shExec(StringBuilder s, Context c, Boolean su) {
    get_assetsScript("run", c, s.toString(), "");
    if (isSystemApp(c)) {
        new CMDProcessor2().sh.runWaitFor("busybox chmod 750 " + c.getFilesDir() + "/run");
    } else {//from   www  .  ja  va2  s . co m
        new CMDProcessor2().su.runWaitFor("busybox chmod 750 " + c.getFilesDir() + "/run");
    }
    CMDProcessor2.CommandResult cr = null;
    if (su && !isSystemApp(c))
        cr = new CMDProcessor2().su.runWaitFor(c.getFilesDir() + "/run");
    else
        cr = new CMDProcessor2().sh.runWaitFor(c.getFilesDir() + "/run");
    if (cr.success()) {
        return cr.stdout;
    } else {
        Log.d(TAG, "execute: " + cr.stderr);
        return null;
    }
}

From source file:me.tatarka.support.internal.job.JobStore.java

public static JobStore initAndGet(Context context) {
    synchronized (sSingletonLock) {
        if (sSingleton == null) {
            sSingleton = new JobStore(context, context.getFilesDir());
        }//from  w  ww  .j ava2s . c o  m
        return sSingleton;
    }
}

From source file:com.commonsware.android.diceware.PassphraseFragment.java

private static Uri makeLocalCopy(Context ctxt, ContentResolver resolver, Uri document) throws IOException {
    DocumentFileCompat docFile = buildDocFileForUri(ctxt, document);
    Uri result = null;// ww w.ja  v  a 2 s .  c o  m

    if (docFile.getName() != null) {
        String ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(docFile.getType());

        if (ext != null) {
            ext = "." + ext;
        }

        File f = File.createTempFile("cw_", ext, ctxt.getFilesDir());

        docFile.copyTo(f);
        result = Uri.fromFile(f);
    }

    return (result);
}

From source file:com.brewcrewfoo.performance.util.Helpers.java

public static String shExec(StringBuilder s, Context c, Boolean su) {
    //final String dn=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+TAG+"/logs";
    //new CMDProcessor().sh.runWaitFor("busybox mkdir -p "+dn );
    get_assetsScript("run", c, "", s.toString());
    new CMDProcessor().sh.runWaitFor("busybox chmod 750 " + c.getFilesDir() + "/run");
    CMDProcessor.CommandResult cr = null;
    if (su) {/*w  w  w.  j  av a2 s  . co  m*/
        //cr=new CMDProcessor().su.runWaitFor(c.getFilesDir()+"/run > " + dn + "/run.log 2>&1");
        cr = new CMDProcessor().su.runWaitFor(c.getFilesDir() + "/run");
    } else {
        //cr=new CMDProcessor().sh.runWaitFor(c.getFilesDir()+"/run > " + dn + "/run.log 2>&1");
        cr = new CMDProcessor().sh.runWaitFor(c.getFilesDir() + "/run");
    }
    if (cr.success()) {
        return cr.stdout;
    } else {
        Log.d(TAG, "execute run error: " + cr.stderr);
        return "nok";
    }
}

From source file:eu.mhutti1.utils.storage.StorageDeviceUtils.java

public static ArrayList<StorageDevice> getStorageDevices(Context context, boolean writable) {
    mStorageDevices = new ArrayList<>();

    // Add as many possible mount points as we know about

    // Only add this device if its very likely that we have missed a users sd card
    if (Environment.isExternalStorageEmulated()) {
        // This is our internal storage directory
        mStorageDevices.add(new StorageDevice(
                generalisePath(Environment.getExternalStorageDirectory().getPath(), writable), true));
    } else {//from ww w . j ava2s. co  m
        // This is the internal directory of our app that only we can write to
        mStorageDevices.add(new StorageDevice(context.getFilesDir().getPath(), true));
        // This is an external storage directory
        mStorageDevices.add(new StorageDevice(
                generalisePath(Environment.getExternalStorageDirectory().getPath(), writable), false));
    }

    // These are possible manufacturer sdcard mount points

    String[] paths = ExternalPaths.getPossiblePaths();

    for (String path : paths) {
        if (path.endsWith("*")) {
            File root = new File(path.substring(0, path.length() - 1));
            File[] directories = root.listFiles(new FileFilter() {
                @Override
                public boolean accept(File file) {
                    return file.isDirectory();
                }
            });
            if (directories != null) {
                for (File dir : directories) {
                    mStorageDevices.add(new StorageDevice(dir, false));
                }
            }
        } else {
            mStorageDevices.add(new StorageDevice(path, false));
        }
    }

    // Iterate through any sdcards manufacturers may have specified in Kitkat+ and add them
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        for (File file : context.getExternalFilesDirs("")) {
            if (file != null) {
                mStorageDevices.add(new StorageDevice(generalisePath(file.getPath(), writable), false));
            }
        }
    }

    // Check all devices exist, we can write to them if required and they are not duplicates
    return checkStorageValid(writable);
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java

public static File getMediaStageDirectory(Context context) {
    File directory = new File(context.getFilesDir(), "media");
    if (!directory.exists()) {
        directory.mkdirs();//from  ww w  .j a  va  2s.  c  o  m
    }

    return directory;
}