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.matrix.matrixandroidsdk.db.ConsoleMediasCache.java

/**
 * Replace a media cache by a file content.
 * MediaUrl is the same model as the one used in loadBitmap.
 * @param context the context//from   w w w .ja  v  a  2 s . c  om
 * @param mediaUrl the mediaUrl
 * @param fileUrl the file which replaces the cached media.
 * @param width the expected image width
 * @param height the expected image height
 */
public static void saveFileMediaForUrl(Context context, String mediaUrl, String fileUrl, int width,
        int height) {
    String downloadableUrl = downloadableUrl(context, mediaUrl, width, height);
    String filename = BitmapWorkerTask.buildFileName(downloadableUrl);

    try {
        // delete the current content
        File destFile = new File(context.getFilesDir(), filename);

        if (destFile.exists()) {
            try {
                destFile.delete();
            } catch (Exception e) {
            }
        }

        Uri uri = Uri.parse(fileUrl);
        File srcFile = new File(uri.getPath());
        srcFile.renameTo(destFile);

    } catch (Exception e) {

    }
}

From source file:com.gelakinetic.mtgfam.helpers.ZipUtils.java

/**
 * Unzip a file directly into getFilesDir()
 *
 * @param zipFile The zip file to unzip/*from   w w w  .  j av a 2  s.c o  m*/
 * @param context The application context, for getting files and the like
 * @throws IOException Thrown if something goes wrong with unzipping and writing
 */
private static void unZipIt(ZipFile zipFile, Context context) throws IOException {
    Enumeration<? extends ZipEntry> entries;

    entries = zipFile.entries();

    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();

        if (entry.isDirectory()) {
            /* Assume directories are stored parents first then children.
             * This is not robust, just for demonstration purposes. */
            if (!(new File(entry.getName())).mkdir()) {
                return;
            }
            continue;
        }
        String[] path = entry.getName().split("/");
        String pathCat = "";
        if (path.length > 1) {
            for (int i = 0; i < path.length - 1; i++) {
                pathCat += path[i] + "/";
                File tmp = new File(context.getFilesDir(), pathCat);
                if (!tmp.exists()) {
                    if (!tmp.mkdir()) {
                        return;
                    }
                }
            }
        }

        InputStream in = zipFile.getInputStream(entry);
        OutputStream out;
        if (entry.getName().contains("_preferences.xml")) {
            String sharedPrefsDir = context.getFilesDir().getPath();
            sharedPrefsDir = sharedPrefsDir.substring(0, sharedPrefsDir.lastIndexOf("/")) + "/shared_prefs/";

            out = new BufferedOutputStream(new FileOutputStream(new File(sharedPrefsDir, entry.getName())));
        } else {
            out = new BufferedOutputStream(
                    new FileOutputStream(new File(context.getFilesDir(), entry.getName())));
        }
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
        }

        in.close();
        out.close();
    }

    zipFile.close();
}

From source file:com.grarak.kerneladiutor.utils.database.PerAppDB.java

public PerAppDB(Context context) {
    super(context.getFilesDir() + "/per_app.json", 1);
}

From source file:com.grarak.kerneladiutor.database.tools.customcontrols.Controls.java

public Controls(Context context) {
    super(context.getFilesDir() + "/controls.json", 1);
}

From source file:com.pdftron.pdf.utils.Utils.java

public static String copyResourceToTempFolder(Context context, int resId, boolean force, String resourceName)
        throws PDFNetException {
    if (context == null) {
        throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "copyResourceToTempFolder()",
                "Context cannot be null to initialize resource file.");
    } else {//  w  w w.  j  ava 2  s.c o m
        File resFile = new File(context.getFilesDir() + File.separator + "resourceName");
        if (!resFile.exists() || force) {
            File filesDir = context.getFilesDir();
            StatFs stat = new StatFs(filesDir.getPath());
            long size = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
            if (size < 2903023L) {
                throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "copyResourceToTempFolder()",
                        "Not enough space available to copy resources file.");
            }

            Resources rs = context.getResources();

            try {
                InputStream e = rs.openRawResource(resId);
                FileOutputStream fos = context.openFileOutput(resourceName, 0);
                byte[] buffer = new byte[1024];

                int read;
                while ((read = e.read(buffer)) != -1) {
                    fos.write(buffer, 0, read);
                }

                e.close();
                fos.flush();
                fos.close();

            } catch (Resources.NotFoundException var13) {
                throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()",
                        "Resource file ID does not exist.");
            } catch (FileNotFoundException var14) {
                throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()",
                        "Resource file not found.");
            } catch (IOException var15) {
                throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()",
                        "Error writing resource file to internal storage.");
            } catch (Exception var16) {
                throw new PDFNetException("", 0L, "com.pdftron.pdf.PDFNet", "initializeResource()",
                        "Unknown error.");
            }
        }

        return context.getFilesDir().getAbsolutePath();
    }
}

From source file:com.grarak.kerneladiutor.utils.database.ProfileDB.java

public ProfileDB(Context context) {
    super(context.getFilesDir() + "/profiles.json", 1);
}

From source file:com.grarak.kerneladiutor.database.tools.profiles.Profiles.java

public Profiles(Context context) {
    super(context.getFilesDir() + "/profiles.json", VERSION);
}

From source file:com.farmerbb.secondscreen.util.U.java

private static String[][] listProfiles(Context context, boolean fakeEntry, String fakeEntryValue,
        int fakeEntryTitle) {
    // Get number of files
    int numOfFiles = getNumOfFiles(context.getFilesDir());
    int numOfProfiles = numOfFiles;

    // Get array of filenames
    String[] listOfFiles;//from   ww w.  ja v a2s.  co m
    ArrayList<String> listOfProfiles = new ArrayList<>();

    if (fakeEntry)
        listOfFiles = getListOfProfiles(context.getFilesDir(), fakeEntryValue);
    else
        listOfFiles = getListOfProfiles(context.getFilesDir());

    // Remove any files from the list that aren't profiles
    for (int i = 0; i < numOfFiles; i++) {
        if (NumberUtils.isNumber(listOfFiles[i]) && !listOfFiles[i].equals(fakeEntryValue))
            listOfProfiles.add(listOfFiles[i]);
        else
            numOfProfiles--;
    }

    if (numOfProfiles == 0)
        return null;
    else {
        // Get "fake" number of files, if applicable
        int fakeNumOfProfiles;
        if (fakeEntry)
            fakeNumOfProfiles = numOfProfiles + 1;
        else
            fakeNumOfProfiles = numOfProfiles;

        // Create arrays of profile lists
        String[] listOfProfilesByDate = new String[fakeNumOfProfiles];
        String[] listOfProfilesByName = new String[fakeNumOfProfiles];

        String[] listOfTitlesByDate = new String[fakeNumOfProfiles];
        String[] listOfTitlesByName = new String[fakeNumOfProfiles];

        for (int i = 0; i < numOfProfiles; i++) {
            listOfProfilesByDate[i] = listOfProfiles.get(i);
        }

        // Get array of first lines of each profile
        for (int i = 0; i < numOfProfiles; i++) {
            try {
                listOfTitlesByDate[i] = getProfileTitle(context, listOfProfilesByDate[i]);
            } catch (IOException e) {
                showToast(context, R.string.error_loading_list);
            }
        }

        // Add fake entry, if applicable
        if (fakeEntry)
            listOfTitlesByDate[numOfProfiles] = " " + context.getResources().getString(R.string.bullet) + " "
                    + context.getResources().getString(fakeEntryTitle) + " "
                    + context.getResources().getString(R.string.bullet);

        // Sort alphabetically
        // Copy titles array
        System.arraycopy(listOfTitlesByDate, 0, listOfTitlesByName, 0, fakeNumOfProfiles);

        // Sort titles
        Arrays.sort(listOfTitlesByName);

        // Initialize profiles array
        for (int i = 0; i < fakeNumOfProfiles; i++)
            listOfProfilesByName[i] = "new";

        // Copy filenames array with new sort order of titles and nullify date arrays
        for (int i = 0; i < fakeNumOfProfiles; i++) {
            for (int j = 0; j < fakeNumOfProfiles; j++) {
                if (listOfTitlesByName[i].equals(listOfTitlesByDate[j])
                        && listOfProfilesByName[i].equals("new")) {
                    listOfProfilesByName[i] = listOfProfilesByDate[j];
                    listOfProfilesByDate[j] = "";
                    listOfTitlesByDate[j] = "";
                }
            }
        }

        if (fakeEntry)
            listOfProfilesByName[0] = fakeEntryValue;

        return new String[][] { listOfProfilesByName, listOfTitlesByName };
    }
}

From source file:hochschuledarmstadt.photostream_tools.ImageCacher.java

private File concatImageFilePath(Context context, String imageFileName) {
    File file = new File(context.getFilesDir(), imageFileName);
    if (!file.exists() && isExternalStorageAccessible()) {
        File pictureDirectory = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if (!pictureDirectory.exists())
            pictureDirectory.mkdir();// w w w  . j a  v  a  2s  .c o  m
        file = new File(pictureDirectory, imageFileName);
    }
    return file;
}

From source file:com.scanvine.android.util.SVDownloadManager.java

public boolean downloadExistsFor(Context context, Story story) {
    File download = new File(context.getFilesDir(), story.getFilename());
    return download.exists() && download.length() > 0;
}