List of usage examples for android.content Context getDir
public abstract File getDir(String name, @FileMode int mode);
From source file:Main.java
public static File getFilePath(Context context, String dirname, String filename) { File cascadeDir = context.getDir(dirname, Context.MODE_PRIVATE); return new File(cascadeDir, filename); }
From source file:Main.java
private static String getInternalStoragePathByContext(String path, Context context) { File dirPath = context.getDir(path, Context.MODE_PRIVATE); if (dirPath.exists()) { dirPath.mkdir();//from w w w .ja v a 2 s.c o m } return dirPath.getAbsolutePath(); }
From source file:Main.java
public static void writeDataFile(String filename, Context context, Object object) { File file = new File(context.getDir("data", 0), filename); try {/*from w w w .ja v a2s. com*/ ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file, false)); outputStream.writeObject(object); outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static File getFilesDir(Context context) { File result = context.getFilesDir(); if (result == null) { result = context.getDir("files", 0); }/*from w w w .j ava2 s . c om*/ if (result == null) { result = new File("/data/data/" + context.getPackageName() + "/app_files"); } if (!result.exists()) { result.mkdirs(); } return result; }
From source file:Main.java
public static File getCacheDir(Context context) { File result = context.getCacheDir(); if (result == null) { result = context.getDir("cache", 0); }/*from w w w .java2 s . com*/ if (result == null) { result = new File("/data/data/" + context.getPackageName() + "/app_cache"); } if (!result.exists()) { result.mkdirs(); } return result; }
From source file:Main.java
public static File createCacheDir(Context context, String dirName) { File preparedDir;/*from w w w . ja v a2 s . co m*/ if (android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment.getExternalStorageState())) { preparedDir = context.getDir(dirName /* + UUID.randomUUID().toString()*/, Context.MODE_PRIVATE); Log.i(TAG, "Cache dir initialized at SD card " + preparedDir.getAbsolutePath()); } else { preparedDir = context.getCacheDir(); Log.i(TAG, "Cache dir initialized at phone storage " + preparedDir.getAbsolutePath()); } if (!preparedDir.exists()) { Log.i(TAG, "Cache dir not existed, creating"); preparedDir.mkdirs(); } return preparedDir; }
From source file:com.amazonaws.utilities.Util.java
/** * Copies the data from the passed in Uri, to a new file for use with the * Transfer Service/*from ww w .j a v a 2 s .c om*/ * * @param context * @param uri * @return * @throws IOException */ public static File copyContentUriToFile(Context context, Uri uri) throws IOException { InputStream is = context.getContentResolver().openInputStream(uri); File copiedData = new File(context.getDir("SampleImagesDir", Context.MODE_PRIVATE), UUID.randomUUID().toString()); copiedData.createNewFile(); FileOutputStream fos = new FileOutputStream(copiedData); byte[] buf = new byte[2046]; int read = -1; while ((read = is.read(buf)) != -1) { fos.write(buf, 0, read); } fos.flush(); fos.close(); return copiedData; }
From source file:com.galactogolf.genericobjectmodel.levelloader.LevelSet.java
public static LevelSet loadLevelSetFromInternalStorage(String levelFilename, Context context) throws LevelLoadingException { File levelsDir = context.getDir(GameConstants.LOCATION_OF_LEVELS_CREATED_BY_USER, Context.MODE_PRIVATE); LevelSet levelSet;//ww w.ja v a2 s . c om File f = null; try { File[] files = levelsDir.listFiles(); for (int i = 0; i < files.length; i++) { if (files[i].getName().equals(levelFilename)) { f = files[i]; } } if (f == null) { throw new LevelLoadingException("Level not found!"); } } catch (Exception ex) { Log.e("General exception", ""); return null; } FileInputStream input = null; try { input = new FileInputStream(f); } catch (FileNotFoundException e) { Log.e("File loading error", e.getMessage()); throw new LevelLoadingException(e.getMessage()); } levelSet = LoadLevelSetFromStream(input); // if this is a file with the id in the name, then check it matches the internal id UUID levelId = null; String[] fileNameParts = f.getName().split("[.]"); if (fileNameParts.length == 4) { levelId = UUID.fromString(fileNameParts[1]); } if (levelId != null && !levelSet.getId().equals(levelId)) { throw new LevelLoadingException("Id within filename and id within file do not agree"); } // manually set the file name to the correct one so prevent conflicts levelSet.setFileName(f.getName()); return levelSet; }
From source file:com.galactogolf.genericobjectmodel.levelloader.LevelSet.java
public static ArrayList<LevelSet> loadAllLevelSetsFromInternalStorage(Context context) throws LevelLoadingException { File levelsDir = context.getDir(GameConstants.LOCATION_OF_LEVELS_CREATED_BY_USER, Context.MODE_PRIVATE); File[] levelSetFiles = levelsDir.listFiles(); ArrayList<LevelSet> levelSets = new ArrayList<LevelSet>(); for (int i = 0; i < levelSetFiles.length; i++) { InputStream input;/*from ww w . jav a 2s.c o m*/ try { input = new FileInputStream(levelSetFiles[i]); } catch (FileNotFoundException e) { Log.e("File loading error", e.getMessage()); throw new LevelLoadingException(e.getMessage()); } StringBuilder sb = new StringBuilder(); final char[] buffer = new char[500]; Reader in = new InputStreamReader(input); int read; try { do { read = in.read(buffer, 0, buffer.length); if (read > 0) { sb.append(buffer, 0, read); } } while (read >= 0); } catch (IOException e) { Log.e("File loading error", e.getMessage()); throw new LevelLoadingException(e.getMessage()); } String data = sb.toString(); try { levelSets.add(JSONSerializer.fromLevelSetJSON(new JSONObject(data))); } catch (JSONException e) { Log.e("File loading error", e.getMessage()); throw new LevelLoadingException(e.getMessage()); } } return levelSets; }
From source file:org.runnerup.workout.WorkoutSerializer.java
public static File getFile(Context ctx, String name) { return new File(ctx.getDir(WORKOUTS_DIR, 0).getPath() + File.separator + name); }