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:key.secretkey.utils.PasswordStorage.java

public static File getRepositoryDirectory(Context context) {
    File dir = null;// www . j  a  v a  2  s. c om
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

    if (settings.getBoolean("git_external", false)) {
        String external_repo = settings.getString("git_external_repo", null);
        if (external_repo != null) {
            dir = new File(external_repo);
        }
    } else {
        dir = new File(context.getFilesDir() + "/store");
    }

    return dir;
}

From source file:simonlang.coastdove.usagestatistics.utility.FileHelper.java

public static File getFile(Context context, Directory directory, String appPackageName, String filename) {
    String publicDirectory = context.getString(R.string.external_folder_name);
    File baseDirectory;/*from   w ww . j  av a 2 s.c o  m*/
    String subDirectory;
    switch (directory) {
    case PRIVATE:
        baseDirectory = context.getFilesDir();
        subDirectory = "";
        break;
    case PUBLIC:
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.e("FileHelper", "External storage is not mounted, failing.");
            return null;
        }
        baseDirectory = Environment.getExternalStoragePublicDirectory(publicDirectory);
        subDirectory = "";
        break;
    case PRIVATE_PACKAGE:
        baseDirectory = context.getFilesDir();
        if (appPackageName == null)
            throw new IllegalArgumentException("appPackageName must not be null");
        subDirectory = appPackageName + "/";
        break;
    case PUBLIC_PACKAGE:
        if (appPackageName == null)
            throw new IllegalArgumentException("appPackageName must not be null");
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            Log.e("FileHelper", "External storage is not mounted, failing.");
            return null;
        }
        baseDirectory = Environment.getExternalStoragePublicDirectory(publicDirectory);
        subDirectory = appPackageName + "/";
        break;
    default:
        throw new IllegalArgumentException("directory must be specified (see FileHelper.Directory)");
    }

    File file = new File(baseDirectory, subDirectory + filename);
    return file;
}

From source file:pt.ubi.di.pdm.swipe.CollectionDemoActivity.java

private static Vector<String> directoryListofFiles(Context ctx) {
    Vector<String> all_texts = new Vector<>();
    File dirFiles = ctx.getFilesDir();
    for (String strFile : dirFiles.list()) {
        all_texts.add(strFile);/*from   w w  w  . ja v a2s. co  m*/
        Log.i("TAG", strFile);
    }
    return all_texts;
}

From source file:com.github.chenxiaolong.dualbootpatcher.patcher.PatcherUtils.java

public synchronized static void extractPatcher(Context context) {
    for (File d : context.getCacheDir().listFiles()) {
        if (d.getName().startsWith("DualBootPatcherAndroid") || d.getName().startsWith("tmp")
                || d.getName().startsWith("data-")) {
            org.apache.commons.io.FileUtils.deleteQuietly(d);
        }//from  ww  w  . j a v  a2s  .  c  om
    }
    for (File d : context.getFilesDir().listFiles()) {
        if (d.isDirectory()) {
            for (File t : d.listFiles()) {
                if (t.getName().contains("tmp")) {
                    org.apache.commons.io.FileUtils.deleteQuietly(t);
                }
            }
        }
    }

    File targetFile = getTargetFile(context);
    File targetDir = getTargetDirectory(context);

    if (BuildConfig.BUILD_TYPE.equals("debug") || !targetDir.exists()) {
        try {
            FileUtils.extractAsset(context, sTargetFile, targetFile);
        } catch (IOException e) {
            throw new IllegalStateException("Failed to extract data archive from assets", e);
        }

        // Remove all previous files
        for (File d : context.getFilesDir().listFiles()) {
            org.apache.commons.io.FileUtils.deleteQuietly(d);
        }

        try {
            LibMiscStuff.extractArchive(targetFile.getAbsolutePath(), context.getFilesDir().getAbsolutePath());
        } catch (IOException e) {
            throw new IllegalStateException("Failed to extract data archive", e);
        }

        // Delete archive
        targetFile.delete();
    }
}

From source file:com.aps490.drdc.prototype.MainActivity.java

public static void initNativeLib(Context context) {
    try {//w ww . j  a  v a 2s . co  m
        // Try loading our native lib, see if it works...
        System.loadLibrary("libarchitect");
    } catch (UnsatisfiedLinkError er) {
        ApplicationInfo appInfo = context.getApplicationInfo();
        String libName = "libarchitect.so";
        String destPath = context.getFilesDir().toString();
        try {
            String soName = destPath + File.separator + libName;
            new File(soName).delete();
            UnzipUtil.extractFile(appInfo.sourceDir, "lib/" + Build.CPU_ABI + "/" + libName, destPath);
            System.load(soName);
        } catch (IOException e) {
            // extractFile to app files dir did not work. Not enough space? Try elsewhere...
            destPath = context.getExternalCacheDir().toString();
            // Note: location on external memory is not secure, everyone can read/write it...
            // However we extract from a "secure" place (our apk) and instantly load it,
            // on each start of the app, this should make it safer.
            String soName = destPath + File.separator + libName;
            new File(soName).delete(); // this copy could be old, or altered by an attack
            try {
                UnzipUtil.extractFile(appInfo.sourceDir, "lib/" + Build.CPU_ABI + "/" + libName, destPath);
                System.load(soName);
            } catch (IOException e2) {
                Log.e("AMMAR:", "Exception in InstallInfo.init(): " + e);
                e.printStackTrace();
            }
        }
    }
}

From source file:mx.klozz.xperience.tweaker.helpers.Helpers.java

public static String shExec(StringBuilder s, Context c, Boolean su) {
    get_assetsScript("run", c, "", s.toString());
    new CMDProcessor().sh.runWaitFor("busybox chmod 750 " + c.getFilesDir() + "/run");
    CMDProcessor.CommandResult CommandR = null;
    if (su) {//from  w w  w.j av  a 2s .c  o  m
        CommandR = new CMDProcessor().su.runWaitFor(c.getFilesDir() + "/run");
    } else {
        CommandR = new CMDProcessor().sh.runWaitFor(c.getFilesDir() + "/run");
    }
    if (CommandR.success()) {
        return CommandR.stdout;
    } else {
        Log.d(TAG, "execute run error: " + CommandR.stderr);
        return "nok";
    }
}

From source file:mx.klozz.xperience.tweaker.helpers.Helpers.java

public static String readCPU(Context context, int i) {
    Helpers.get_assetsScript("utils", context, "", "");
    new CMDProcessor().sh.runWaitFor("busybox chmod 750 " + context.getFilesDir() + "/utils");
    CMDProcessor.CommandResult CommandR = new CMDProcessor().su
            .runWaitFor(context.getFilesDir() + "/utils -getcpu " + i);
    if (CommandR.success())
        return CommandR.stdout;
    else/*w w w  .  ja  v  a  2  s .c o  m*/
        return null;
}

From source file:com.hybris.mobile.logging.ExceptionHandler.java

public static boolean register(Context context) {
    LoggingUtils.i(LOG_TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {/* w  w w .  j  a v  a  2s  .c o  m*/
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        RA.APP_VERSION = pi.versionName;
        // Package name
        RA.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        RA.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        RA.PHONE_MODEL = android.os.Build.MODEL;
        // Android version
        RA.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        LoggingUtils.e(LOG_TAG,
                "No package found for \"" + context.getPackageName() + "\"" + e.getLocalizedMessage(), context);
    }

    LoggingUtils.i(LOG_TAG, "TRACE_VERSION: " + RA.TraceVersion);
    LoggingUtils.d(LOG_TAG, "APP_VERSION: " + RA.APP_VERSION);
    LoggingUtils.d(LOG_TAG, "APP_PACKAGE: " + RA.APP_PACKAGE);
    LoggingUtils.d(LOG_TAG, "FILES_PATH: " + RA.FILES_PATH);
    LoggingUtils.d(LOG_TAG, "URL: " + RA.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) {
                LoggingUtils.d(LOG_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:nitezh.ministock.UserData.java

public static void cleanupPreferenceFiles(Context context) {
    // Remove old preferences if we are upgrading
    ArrayList<String> l = new ArrayList<String>();

    // Shared preferences is never deleted
    l.add(context.getString(R.string.prefs_name) + ".xml");
    for (int id : UserData.getAppWidgetIds2(context))
        l.add(context.getString(R.string.prefs_name) + id + ".xml");

    // Remove files we do not have an active widget for
    String appDir = context.getFilesDir().getParentFile().getPath();
    File f_shared_preferences = new File(appDir + "/shared_prefs");

    // Check if shared_preferences exists
    // TODO: Work out why this is ever null and an alternative strategy
    if (f_shared_preferences.exists())
        for (File f : f_shared_preferences.listFiles())
            if (!l.contains(f.getName()))
                //noinspection ResultOfMethodCallIgnored
                f.delete();//  w w  w.ja v a  2 s .  c  om
}

From source file:com.l.notel.notel.org.redpin.android.util.ExceptionReporter.java

/**
 * Register handler for unhandled exceptions.
 * /*w  w  w  . ja v a2 s . co  m*/
 * @param context
 */
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);
        E.APP_VERSION = pi.versionName;
        // Package name
        E.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        E.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        E.PHONE_MODEL = android.os.Build.MODEL;
        // Android version
        E.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Log.i(TAG, "TRACE_VERSION: " + E.TraceVersion);
    Log.d(TAG, "APP_VERSION: " + E.APP_VERSION);
    Log.d(TAG, "APP_PACKAGE: " + E.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + E.FILES_PATH);
    Log.d(TAG, "URL: " + E.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 ExceptionHandler)) {
                // Register default exceptions handler
                Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(currentHandler));
            }
        }
    }.start();

    return stackTracesFound;
}