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.pseudosudostudios.jdd.utils.ScoreSaves.java

private static void writeFile(Context c, List<Entry> entries) {
    Collections.sort(entries);// w  w w.java2 s . co  m
    File file = new File(c.getFilesDir(), fileName);
    try {
        file.createNewFile();
        JSONArray array = new JSONArray();
        for (Entry e : entries)
            array.put(makeJSONObject(e));
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        writer.write(array.toString());
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }
}

From source file:Main.java

public static String readFile(Context ctx, String filename) {
    FileInputStream fis = null;//from ww w  .jav a  2 s  .  c  o  m
    String line = null;
    StringBuilder sb = new StringBuilder();
    try {
        fis = new FileInputStream(ctx.getFilesDir() + File.separator + filename);
        InputStreamReader inputStreamReader = new InputStreamReader(fis);
        @SuppressWarnings("resource")
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }
    } catch (Exception e) {
        //e.printStackTrace();
        return null;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException e) {
            return null;
        }
    }

    return sb.toString();
}

From source file:com.pseudosudostudios.jdd.utils.ScoreSaves.java

/**
 * @param c the application's context that owns the data file
 * @return a List of games saved in the file
 *//*from   w  ww.  j  av  a 2  s . c om*/
public static List<Entry> getSaves(Context c) {
    List<Entry> entries = new ArrayList<Entry>();
    File f = new File(c.getFilesDir(), fileName);
    if (!f.exists() || f.isDirectory()) {
        return new ArrayList<Entry>();
    }
    try {
        BufferedReader reader = new BufferedReader(new FileReader(f));
        StringBuffer buff = new StringBuffer();
        while (reader.ready())
            buff.append(reader.readLine());
        reader.close();
        JSONArray array = new JSONArray(buff.toString());
        for (int i = 0; i < array.length(); i++) {
            Entry e = makeEntry(array.getJSONObject(i));
            if (e.getScore() != IGNORE_SCORE)
                entries.add(e);
        }

        Collections.sort(entries);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return entries;
}

From source file:Main.java

private static File getStorageFolder(Context context) {
    // Get external storage folder
    File storageFolder = context.getExternalFilesDir(null);
    if (storageFolder == null) {
        // If failed, get internal storage folder
        storageFolder = context.getFilesDir();
    }/* w  w  w .j  a  va2  s . co  m*/
    return storageFolder;
}

From source file:Main.java

private static File getStorageDir(Context context) {
    if (storageDir == null) {
        File file = Environment.getExternalStorageDirectory();
        if (file.exists()) {
            return file;
        }//from   ww  w  .  j ava2 s .c  o  m

        storageDir = context.getFilesDir();
    }

    return storageDir;
}

From source file:Main.java

public static KeyPair loadKeyPair(Context context, String name)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    // Read Public Key.
    File filePublicKey = new File(context.getFilesDir(), name + "_public.key");
    FileInputStream fis = new FileInputStream(filePublicKey);
    byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    fis.read(encodedPublicKey);//w w w  .  ja  va  2  s .  c  o  m
    fis.close();

    // Read Private Key.
    File filePrivateKey = new File(context.getFilesDir(), name + "_private.key");
    fis = new FileInputStream(filePrivateKey);
    byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    fis.read(encodedPrivateKey);
    fis.close();

    // Generate KeyPair.
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey);
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);

    PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return new KeyPair(publicKey, privateKey);
}

From source file:com.dev.pygmy.util.Utils.java

public static String getGamePath(Context context, String... suffixes) {
    StringBuilder sb = new StringBuilder();
    sb.append(context.getFilesDir().getPath());
    for (String suffix : suffixes) {
        sb.append("/");
        sb.append(suffix);//from  w  w  w . j  av  a2s .c  o m
    }
    return sb.toString();
}

From source file:com.google.codelab.networkmanager.CodelabUtil.java

public static List<TaskItem> getTaskItemsFromFile(Context context) {
    List<TaskItem> taskItems = new ArrayList<>();
    File taskFile = new File(context.getFilesDir(), FILE_NAME);
    if (!taskFile.exists()) {
        return taskItems;
    }//  w w  w. j  a v a2  s . c  o m
    try {
        String taskStr = IOUtils.toString(new FileInputStream(taskFile));
        taskItems.addAll(taskItemsFromString(taskStr));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return taskItems;
}

From source file:net.naonedbus.utils.InfoDialogUtils.java

/**
 * Crer le rpertoire servant  stocker les id des messages dj affichs
 * /* w  ww.j  av a  2s . c om*/
 * @param context
 */
private static void createDir(final Context context) {
    final File file = new File(context.getFilesDir(), MESSAGE_FOLDER);
    if (!file.exists()) {
        file.mkdirs();
    }
}

From source file:Main.java

public static void initWidgetOneFile(Context context, String appId) {
    String root = null;//from   w  ww  .  j a  va 2s . c  o m
    appId += "/";
    if (sdCardIsWork()) {
        root = getSdCardRootPath();
    } else {
        root = context.getFilesDir().getAbsolutePath() + "/";
    }
    String[] fileDir = { root + F_APP_PATH + appId, root + F_WIDGET_PATH,
            root + F_APP_PATH + appId + F_APP_VIDEO, root + F_APP_PATH + appId + F_APP_PHOTO,
            root + F_APP_PATH + appId + F_APP_AUDIO, root + F_APP_PATH + appId + F_APP_MYSPACE };
    int size = fileDir.length;
    for (int i = 0; i < size; i++) {
        File file = new File(fileDir[i]);
        if (!file.exists()) {
            file.mkdirs();
        }
    }
    String noMediaStr = root + F_BASE_WGT_PATH + ".nomedia";
    File noMedia = new File(noMediaStr);
    if (!noMedia.exists()) {
        try {
            noMedia.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}