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:org.montanafoodhub.base.get.ItemHub.java

protected HashMap<String, Item> readFromFile(Context context) {
    HashMap<String, Item> myItemMap = new HashMap<String, Item>();
    try {/*from www  .j av  a  2  s.  co m*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myItemMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of items loaded: " + myItemMap.size());
    return myItemMap;
}

From source file:io.github.data4all.util.Gallery.java

/**
 * Constructs a gallery to read from and write to.
 * //from   w  ww . j  av  a 2s.c  o m
 * @param context
 *            The context of this gallery
 */
public Gallery(Context context) {
    workingDirectory = new File(context.getFilesDir(), DIRECTORY_NAME);
}

From source file:org.montanafoodhub.base.get.CertificationHub.java

protected HashMap<String, Certification> readFromFile(Context context) {
    HashMap<String, Certification> myCertificationMap = new HashMap<String, Certification>();
    try {/*from ww  w . ja v a  2s  .  c om*/
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myCertificationMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of certifications loaded: " + myCertificationMap.size());
    return myCertificationMap;
}

From source file:com.mikecorrigan.trainscorekeeper.Game.java

private boolean deleteSavedGame(final Context context) {
    Log.vc(VERBOSE, TAG, "deleteSavedGame: context=" + context);

    String path = context.getFilesDir() + File.separator + SAVED_FILE;
    File file = new File(path);
    return file.delete();
}

From source file:org.montanafoodhub.base.get.BuyerHub.java

protected HashMap<String, Buyer> readFromFile(Context context) {
    HashMap<String, Buyer> myBuyerMap = new HashMap<String, Buyer>();
    try {//from w  w w .j  a  v  a 2s.com
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myBuyerMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of buyers loaded: " + myBuyerMap.size());
    return myBuyerMap;
}

From source file:ics4u.ics4u_final_project.Database.java

/**
 * Imports the recipes from the recipe folder
 *
 * @return an array list containing all the recipes in the folder
 */// w ww . j av a2  s .  c  o m
public static ArrayList<Recipe> importRecipes(Context c) {
    //create a list of the recipes
    ArrayList<Recipe> database = new ArrayList<>();
    //the action changes depending on if its the first run or not
    if (MainActivity.prefs.getBoolean("firstrun", true)) {
        //set it to not being the first run anymore
        MainActivity.prefs.edit().putBoolean("firstrun", false).commit();
        //add the provided recipe(s)
        database.add(open(c.getResources().openRawResource(R.raw.banana_lentil_muffins)));
        database.add(open(c.getResources().openRawResource(R.raw.cheddar_lentil_crispies)));
        //create the recipes folder in the data folder
        File recipeFolder = new File(c.getFilesDir() + "/recipes/");
        recipeFolder.mkdir();
        //create the folder for the PDFs, if it's not already created
        File pdfFolder = new File("/sdcard/Recipes/");
        int permissionCheck = ContextCompat.checkSelfPermission(c, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            if (!pdfFolder.isDirectory()) {
                pdfFolder.mkdir();
            }
        } else {
            Toast.makeText(c, "Permission denied. Please grant storage permissions", Toast.LENGTH_LONG).show();
        }
        //save each of the imported recipes
        try {
            database.get(0).save(new File(c.getFilesDir() + "/recipes/" + 0 + ".xml"));
            database.get(1).save(new File(c.getFilesDir() + "/recipes/" + 1 + ".xml"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        //get a list of the files in the recipes folder
        File folder = new File(c.getFilesDir() + "/recipes/");
        File[] listOfFiles = folder.listFiles();
        //import each of the recipes
        for (File file : listOfFiles) {
            if (file.isFile() && file.getName().substring(file.getName().indexOf(".")).equals(".xml")) {
                try {
                    database.add(open(new FileInputStream(file)));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return database;
}

From source file:com.mikecorrigan.trainscorekeeper.Game.java

public boolean read(final Context context) {
    Log.vc(VERBOSE, TAG, "read: context=" + context);

    String path = context.getFilesDir() + File.separator + SAVED_FILE;
    File file = new File(path);

    JSONObject jsonRoot = JsonUtils.readFromFile(file);
    if (jsonRoot == null) {
        Log.e(TAG, "read: failed to read root");
        return false;
    }/*  ww  w . ja v a2 s .  co m*/

    scoreEvents.clear();
    lastScoreEvent = 0;

    try {
        spec = jsonRoot.getString(JsonSpec.GAME_SPEC);
        JSONArray jsonScoreEvents = jsonRoot.getJSONArray(JsonSpec.SCORES_KEY);

        for (int i = 0; i < jsonScoreEvents.length(); i++) {
            JSONObject jsonScoreEvent = jsonScoreEvents.getJSONObject(i);
            ScoreEvent scoreEvent = new ScoreEvent(jsonScoreEvent);
            scoreEvents.add(scoreEvent);

            notifyEnabled(scoreEvent.getPlayerId(), true);
        }
    } catch (JSONException e) {
        Log.th(TAG, e, "read: parse failed");
        return false;
    }

    lastScoreEvent = scoreEvents.size();

    notifyListeners();

    return true;
}

From source file:org.montanafoodhub.base.get.ProducerHub.java

protected HashMap<String, Producer> readFromFile(Context context) {
    HashMap<String, Producer> myProducerMap = new HashMap<String, Producer>();
    try {// w  w w.j av  a  2 s.c  om
        // getItem the time the file was last changed here
        File myFile = new File(context.getFilesDir() + "/" + fileName);
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        String lastRefreshTSStr = sdf.format(myFile.lastModified());
        Log.w(HubInit.logTag, "Using file (" + fileName + ") last modified on : " + lastRefreshTSStr);
        lastRefreshTS = sdf.getCalendar();

        // create products from the file here
        InputStream inputStream = context.openFileInput(fileName);
        if (inputStream != null) {
            parseCSV(myProducerMap, inputStream);
        }
        inputStream.close();
    } catch (FileNotFoundException e) {
        Log.e(HubInit.logTag, "File  (" + fileName + ") not found: " + e.toString());
    } catch (IOException e) {
        Log.e(HubInit.logTag, "Can not read file  (" + fileName + ") : " + e.toString());
    }
    Log.w(HubInit.logTag, "Number of producers loaded: " + myProducerMap.size());
    return myProducerMap;
}

From source file:com.platform.middlewares.plugins.CameraPlugin.java

public byte[] readPictureForId(Context context, String id) {
    Log.i(TAG, "readPictureForId: " + id);
    try {//from   w w w. j  a v  a  2s.c  om
        //create FileInputStream object
        FileInputStream fin = new FileInputStream(
                new File(context.getFilesDir().getAbsolutePath() + "/pictures/" + id + ".jpeg"));

        //create string from byte array
        return IOUtils.toByteArray(fin);

    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found " + e);
    } catch (IOException ioe) {
        Log.e(TAG, "Exception while reading the file " + ioe);
    }
    return null;
}

From source file:ca.ualberta.cs.expenseclaim.dao.ExpenseItemDao.java

private void save(Context context) {
    Collections.sort(itemList, new Comparator<ExpenseItem>() {

        @Override/*from   w ww . j  a  va 2 s  .  co  m*/
        public int compare(ExpenseItem lhs, ExpenseItem rhs) {
            return lhs.getDate().compareTo(rhs.getDate());
        }

    });
    File file = new File(context.getFilesDir(), FILENAME);
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(file);
        for (ExpenseItem i : itemList) {
            JSONObject jo = new JSONObject();
            jo.put("id", i.getId());
            jo.put("claimId", i.getClaimId());
            jo.put("category", i.getCategory());
            jo.put("description", i.getDescription());
            jo.put("date", i.getDate().getTime());
            jo.put("amount", i.getAmount());
            jo.put("unit", i.getUnit());
            writer.println(jo.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }

}