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:Main.java

/**
 * Return a {@link File} pointing to the storage location for map tiles.
 *///from w  w w. j a  v a 2 s.c  o m
public static File getTileFile(Context context, String filename) {
    File folder = new File(context.getFilesDir(), TILE_PATH);
    if (!folder.exists()) {
        folder.mkdirs();
    }
    return new File(folder, filename);
}

From source file:Main.java

private static File getCacheDirImpl(Context c) {
    synchronized (sSync) {
        sCacheDir = new File(c.getFilesDir(), "cache");
        if (sCacheDir.exists() == false) {
            if (sCacheDir.mkdirs() == false) {
                Log.w(TAG, "Unable to create cache directory");
                return null;
            }//  w w  w.  j  a v  a 2 s .  com
        }
    }
    return sCacheDir;
}

From source file:Main.java

public static File getFilesDirectory(Context context) {
    // creates files directory under data/data/package name
    return context.getFilesDir();
}

From source file:Main.java

static void cleanup(Context ctxt) {
    try {//from w w w .ja va2 s.  c  o m
        String[] children = ctxt.getFilesDir().list();

        if (children != null) {
            for (int i = 0; i < children.length; i++) {
                String filename = children[i];
                new File(ctxt.getFilesDir(), filename).delete();
            }
        }
    } catch (Exception ex) {
        // TODO: let the UI know about this via broadcast
        Log.e("QRCodeKeeper-AppUtils", "Exception cleaning up from past exception", ex);
    }
}

From source file:Main.java

public static File getLocalPath(Context context) {
    if (context == null)
        return null;
    File tmp = context.getFilesDir();
    if (tmp != null)
        return tmp.getParentFile();

    return null;//from   w  ww .  ja va2  s . com
}

From source file:Main.java

public static void intArrayToFile(Context myContext, String filename, int[] array) {
    File root = myContext.getFilesDir();
    File current = new File(root, filename);
    current.delete();// ww  w.jav  a  2  s. com
    FileOutputStream outputStream;
    try {
        outputStream = myContext.openFileOutput(filename, Context.MODE_APPEND);
        for (int i : array) {
            String s = "" + i;
            outputStream.write(s.getBytes());
            outputStream.write("\n".getBytes());
        }
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static Object readSeriObject(Context context, String fileName) throws Exception {
    String path = context.getFilesDir() + "/";

    File dir = new File(path);
    dir.mkdirs();//  www  .  j a  v a2 s .  co m
    File file = new File(dir, fileName);
    InputStream is = new FileInputStream(file);

    ObjectInputStream objectInputStream = new ObjectInputStream(is);

    Object o = objectInputStream.readObject();

    return o;

}

From source file:Main.java

public static void copyDB(Context context, String fileName) throws IOException {
    String filePath = context.getFilesDir().getAbsolutePath() + "/" + fileName;
    if (new File(filePath).exists()) {
        return;//from   w  ww  .  j a v  a  2s.  co  m
    }
    FileOutputStream fos = new FileOutputStream(new File(filePath));
    InputStream is = context.getResources().getAssets().open(fileName);
    byte[] buffer = new byte[1024 * 500];
    int count = 0;
    while ((count = is.read(buffer)) > 0) {
        fos.write(buffer, 0, count);
    }
    fos.close();
    is.close();
}

From source file:Main.java

public static File getInternalFileObject(String uri, Context context) {
    if (!uri.contains(File.pathSeparator)) {
        return new File(context.getFilesDir(), uri);
    }//from w  w  w . java2s  .c o  m
    return null;
}

From source file:Main.java

private static InputStream bitmap2InputStream(Context context, Bitmap bitmap) throws IOException {
    File file = new File(context.getFilesDir(), "wallpaper.jpg");
    if (file.exists()) {
        file.delete();//from   w w  w  .ja va2 s. co m
    }
    file.createNewFile();
    FileOutputStream fileOutputStream = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
    fileOutputStream.close();
    return filePath2InputStream(file.getAbsolutePath());
}