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.scanvine.android.util.SVDownloadManager.java

public void deleteDownload(Context context, String fileName) {
    File file = new File(context.getFilesDir(), fileName);
    if (file.exists())
        file.delete();/*ww w  .  j  a  v a  2s  .c  o  m*/
}

From source file:com.nextgis.mobile.util.SelectMapPathPreference.java

@Override
protected void onClick() {
    super.onClick();

    Context context = getContext();
    File path = context.getExternalFilesDir(null);
    if (null == path) {
        path = context.getFilesDir();
    }/*from   w  w w .java 2  s .co  m*/

    LocalResourceSelectDialog dialog = new LocalResourceSelectDialog();
    dialog.setPath(path);
    dialog.setTypeMask(ConstantsUI.FILETYPE_FOLDER);
    dialog.setCanSelectMultiple(false);
    dialog.setOnSelectionListener(this);
    dialog.show(mFragmentManager, ConstantsUI.FRAGMENT_SELECT_RESOURCE);
}

From source file:com.androidzeitgeist.dashwatch.common.IOUtil.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static File getBestAvailableFilesRoot(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        // In KitKat we can query multiple devices.
        // TODO: optimize for stability instead of picking first one
        File[] roots = context.getExternalFilesDirs(null);
        if (roots != null) {
            for (File root : roots) {
                if (root == null) {
                    continue;
                }/*  w ww .ja  v a  2 s .co m*/

                if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(root))) {
                    return root;
                }
            }
        }

    } else if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        // Pre-KitKat, only one external storage device was addressable
        return context.getExternalFilesDir(null);
    }

    // Worst case, resort to internal storage
    return context.getFilesDir();
}

From source file:com.facebook.samples.musicdashboard.Song.java

public File getLocalFile(Context c) {
    if (null == url)
        return null;

    String fileName = getId();//from   w  w  w.ja v a 2 s  .  c om
    return new File(c.getFilesDir() + "/" + fileName);
}

From source file:fr.shywim.antoinedaniel.sync.AppState.java

/**
 * Load AppState from file.//  w ww  . j a v a  2s . co  m
 *
 * @param context a context.
 */
public void load(final Context context) {
    File privDir = context.getFilesDir();
    final File appState = new File(privDir, FILE_NAME);

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                if (appState.exists()) {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(appState));
                    byte[] bytes = new byte[(int) appState.length()];
                    for (int i = 0, temp; (temp = bis.read()) != -1; i++) {
                        bytes[i] = (byte) temp;
                    }

                    bis.close();
                    loadFromBytes(bytes, context, null);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

From source file:es.deustotech.piramide.utils.tts.TextToSpeechWeb.java

private static synchronized void speech(final Context context, final String text, final String language) {
    executor.submit(new Runnable() {
        @Override/*from w w  w.  j av a2 s.com*/
        public void run() {
            try {
                final String encodedUrl = Constants.URL + language + "&q="
                        + URLEncoder.encode(text, Encoding.UTF_8.name());
                final DefaultHttpClient client = new DefaultHttpClient();
                HttpParams params = new BasicHttpParams();
                params.setParameter("http.protocol.content-charset", "UTF-8");
                client.setParams(params);
                final FileOutputStream fos = context.openFileOutput(Constants.MP3_FILE,
                        Context.MODE_WORLD_READABLE);
                try {
                    try {
                        final HttpResponse response = client.execute(new HttpGet(encodedUrl));
                        downloadFile(response, fos);
                    } finally {
                        fos.close();
                    }
                    final String filePath = context.getFilesDir().getAbsolutePath() + "/" + Constants.MP3_FILE;
                    final MediaPlayer player = MediaPlayer.create(context.getApplicationContext(),
                            Uri.fromFile(new File(filePath)));
                    player.start();
                    Thread.sleep(player.getDuration());
                    while (player.isPlaying()) {
                        Thread.sleep(100);
                    }
                    player.stop();

                } finally {
                    context.deleteFile(Constants.MP3_FILE);
                }
            } catch (InterruptedException ie) {
                // ok
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:fr.shywim.antoinedaniel.sync.AppState.java

/**
 * Save the AppState to a file.//from  ww w .  java  2s. co m
 *
 * @param context a context.
 */
public void save(Context context) {
    if (changed) {
        File privDir = context.getFilesDir();
        File appState = new File(privDir, FILE_NAME);

        try {
            byte[] bytes = getAsByteArray();
            if (appState.exists() || (!appState.exists() && appState.createNewFile())) {
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(appState));
                bos.write(bytes);
                bos.flush();
                bos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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

private ExpenseClaimDao(Context context) {
    claimList = new ArrayList<ExpenseClaim>();
    File file = new File(context.getFilesDir(), FILENAME);
    Scanner scanner = null;/*from   ww  w.ja  va  2  s  . com*/
    try {
        scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            JSONObject jo = new JSONObject(scanner.nextLine());
            ExpenseClaim claim = new ExpenseClaim();
            claim.setId(jo.getInt("id"));
            claim.setName(jo.getString("name"));
            claim.setDescription(jo.getString("description"));
            claim.setStartDate(new Date(jo.getLong("startDate")));
            claim.setEndDate(new Date(jo.getLong("endDate")));
            claim.setStatus(jo.getString("status"));
            if (maxId < claim.getId()) {
                maxId = claim.getId();
            }
            claimList.add(claim);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    maxId++;
}

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

private ExpenseItemDao(Context context) {
    itemList = new ArrayList<ExpenseItem>();
    File file = new File(context.getFilesDir(), FILENAME);
    Scanner scanner = null;/*from  ww w.j  a v  a2 s.  c  o  m*/
    try {
        scanner = new Scanner(file);
        while (scanner.hasNextLine()) {
            JSONObject jo = new JSONObject(scanner.nextLine());
            ExpenseItem item = new ExpenseItem();
            item.setId(jo.getInt("id"));
            item.setClaimId(jo.getInt("claimId"));
            item.setCategory(jo.getString("category"));
            item.setDescription(jo.getString("description"));
            item.setDate(new Date(jo.getLong("date")));
            item.setAmount(jo.getDouble("amount"));
            item.setUnit(jo.getString("unit"));
            if (maxId < item.getId()) {
                maxId = item.getId();
            }
            itemList.add(item);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    maxId++;
}

From source file:com.csipsimple.backup.SipSharedPreferencesHelper.java

@SuppressLint("SdCardPath")
private File getPreferenceFile(Context context, String prefName) {
    String finalPath = "shared_prefs/" + prefName + ".xml";
    File f = new File(context.getFilesDir(), "../" + finalPath);
    if (f.exists()) {
        return f;
    }/*from  w w  w  .ja  v  a  2s. com*/
    f = new File("/data/data/" + context.getPackageName() + "/" + finalPath);
    if (f.exists()) {
        return f;
    }
    f = new File("/dbdata/databases/" + context.getPackageName() + "/" + finalPath);
    if (f.exists()) {
        return f;
    }
    return null;
}