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

public static boolean exists(Context context, String fileName) {
    return new File(context.getFilesDir(), fileName).exists();
}

From source file:Main.java

public static void imgCacheWrite(Context context, String cacheImgFileName, String imgBase64Str) {
    File cacheImgFile = new File(context.getFilesDir() + "/" + cacheImgFileName);
    if (cacheImgFile.exists()) {
        cacheImgFile.delete();//from  ww  w  .j  a  v a 2 s .c  om
    }

    FileOutputStream fos;
    try {
        fos = context.openFileOutput(cacheImgFileName, Context.MODE_PRIVATE);
        fos.write(imgBase64Str.getBytes("utf-8"));
        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static String readFromInternalStorage(Context context, String fileName) {

    File file = new File(context.getFilesDir(), fileName);

    try {// w w  w .  j a v a  2  s.co m
        FileInputStream fis = new FileInputStream(file);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        String line;

        StringBuilder stringBuilder = new StringBuilder();
        while ((line = br.readLine()) != null) {
            stringBuilder.append(line);
        }
        String json = stringBuilder.toString();

        br.close();
        in.close();
        fis.close();

        return json;
    } catch (IOException e) {
        Log.e(TAG, e.getMessage(), e);
    }

    return null;
}

From source file:ca.rmen.android.scrumchatter.export.Export.java

/**
 * @return File in the share folder that we can write to before sharing.
 *//*from  ww  w . j ava  2 s  .c  o  m*/
@Nullable
static File getExportFile(Context context, String filename) {
    File exportFolder = new File(context.getFilesDir(), EXPORT_FOLDER_PATH);
    if (!exportFolder.exists() && !exportFolder.mkdirs()) {
        Log.v(TAG, "Couldn't find or create export folder " + exportFolder);
        return null;
    }
    return new File(exportFolder, filename);
}

From source file:Main.java

public static File getDownloadDir(Context context) {
    return isExternalStorageWritable() ? context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
            : context.getFilesDir();
}

From source file:Main.java

static public ArrayList<String> getSavedGestureNames(Context context) {

    ArrayList<String> gestureNames = new ArrayList<>();

    File[] files = context.getFilesDir().listFiles(new FilenameFilter() {
        @Override//  w ww .  j a  va  2s .co  m
        public boolean accept(File dir, String filename) {
            return filename.endsWith(GESTURE_NAME_SUFFIX);
        }
    });

    //return the files without the suffix
    for (File file : files) {
        gestureNames.add(file.getName().substring(0, file.getName().indexOf(GESTURE_NAME_SUFFIX)));
    }

    return gestureNames;
}

From source file:Main.java

/**
 * clean file(/data/data/com.xxx.xxx/files)
 *
 * @param context//from ww  w  .ja va 2 s.  c  o m
 */
public static void cleanFiles(Context context) {
    deleteFilesInDirectory(context.getFilesDir());
}

From source file:Main.java

static public File getPath(Context ctx, boolean externalStorage) {
    if (externalStorage) {
        return ctx.getExternalFilesDir(null);
    } else {//from   w  w w  .  j  a  va 2  s.c o  m
        return ctx.getFilesDir();
    }
}

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

public static boolean deleteFile(Context c) {
    return new File(c.getFilesDir(), fileName).delete();
}

From source file:Main.java

public static File getFilesDir(Context context) {
    File targetDir = context.getExternalFilesDir(null);
    if (targetDir == null || !targetDir.exists()) {
        targetDir = context.getFilesDir();
    }//from  www.j  a  va 2  s.  co  m
    return targetDir;
}