/** * Retrieves cached messages from the cache file and deletes the file. * * @return */ private String[] retrieveCachedMessages() { Log.i(TAG, "Retrieving cached messages"); String cache = ""; try { FileInputStream fis = activity.openFileInput(CACHE_FILE); byte[] bytes = new byte[8192]; if (fis == null) { Log.e(TAG, "Null file stream returned from openFileInput"); return null; } int n = fis.read(bytes); Log.i(TAG, "Read " + n + " bytes from " + CACHE_FILE); cache = new String(bytes, 0, n); fis.close(); activity.deleteFile(CACHE_FILE); messagesCached = 0; Log.i(TAG, "Retrieved cache " + cache); } catch (FileNotFoundException e) { Log.e(TAG, "No Cache file found -- this is not (usually) an error"); return null; } catch (IOException e) { Log.e(TAG, "I/O Error reading from cache file"); e.printStackTrace(); return null; } String messagelist[] = cache.split(MESSAGE_DELIMITER); return messagelist; }
public static Bitmap getBitmapFromId(String id) { try { Bitmap bitmap = BitmapFactory.decodeStream(act.openFileInput(id)); return bitmap; } catch (FileNotFoundException e) { } return null; }
public static void loadSettings(Activity aActivity) { Properties lProps = new Properties(); try { lProps.load(aActivity.openFileInput(CONFIG_FILE)); } catch (Exception ex) { Toast.makeText( aActivity.getApplicationContext(), ex.getClass().getSimpleName() + ":" + ex.getMessage(), Toast.LENGTH_SHORT) .show(); } mURL = (String) lProps.getProperty("url", "ws://localhost:8787"); }
/** * Loads records from the disk, replacing any currently in the database. * * @param activity the activity to use for file IO. */ public void loadRecords(Activity activity) { try { FileInputStream fis = activity.openFileInput(FILENAME); Gson gson = new Gson(); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); RecordDatabase db = gson.fromJson(br, RecordDatabase.class); this.suRecords = db.suRecords; this.muRecords = db.muRecords; fis.close(); } catch (Exception e) { // if we couldn't load them, it probably means they didn't exist. } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments().containsKey(ARG_GROUP)) { // Load the dummy content specified by the fragment // arguments. In a real-world scenario, use a Loader // to load content from a content provider. mGroup = getArguments().getParcelable(ARG_GROUP); if (mGroup != null) { Log.d("GroupDetailFragment", "mGroup old size is " + mGroup.size()); if (mGroup.size() == 0) { // double check to see if anyone is in the group PeopleDatabase db = new PeopleDatabase(getActivity()); db.open(); mGroup.addPeople(db.getPeopleForGroup(mGroup.getId())); db.close(); Log.d("GroupDetailFragment", "mGroup new size is " + mGroup.size()); } Activity activity = this.getActivity(); final String picPath = mGroup.getPicture(); Log.d("GroupDetailFragment", "picPath is " + picPath); if (picPath != null && !picPath.isEmpty()) { ImageView img = (ImageView) activity.findViewById(R.id.toolbar_image); try { Drawable pic = Drawable.createFromStream(activity.openFileInput(picPath), null); img.setImageDrawable(pic); Log.d("GroupDetailFragment", "set background"); } catch (IOException e) { e.printStackTrace(); } } CollapsingToolbarLayout appBarLayout = (CollapsingToolbarLayout) activity.findViewById(R.id.toolbar_layout); if (appBarLayout != null) { appBarLayout.setTitle(mGroup.getName()); appBarLayout.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) {} }); } } } }
@SuppressWarnings("unchecked") private Object getObject(String key, String description, Activity callingActivity) { FileInputStream file = null; ObjectInputStream input = null; try { file = callingActivity.openFileInput(key); input = new ObjectInputStream(file); if (key == COUNT) { return (MyCountDownTimerWrapper) input.readObject(); } else if (key == LIST) { return (Stack<Exercise>) input.readObject(); } else if (key == STACK) { return (Stack<Exercise>) input.readObject(); } else if (key == KEYS) { return (ArrayList<String>) input.readObject(); } else { return (ArrayList<Exercise>) input.readObject(); } } catch (StreamCorruptedException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } catch (OptionalDataException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } catch (IOException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } catch (ClassNotFoundException e) { Log.e(MainMenuActivity.LOG_TAG, "Could not get " + description + " from file"); e.printStackTrace(); } finally { try { if (file != null && input != null) { file.close(); input.close(); } } catch (IOException e) { Log.e( MainMenuActivity.LOG_TAG, "Could not close files while getting " + description + " from file"); e.printStackTrace(); } } return null; }
public boolean load() { try { InputStream is = m_context.openFileInput(PREFS_FILENAME); Properties properties = new Properties(); properties.loadFromXML(is); if (properties.getProperty(PREF_PARANOID) != null) Paranoid = Boolean.parseBoolean(properties.getProperty(PREF_PARANOID)); if (properties.getProperty(PREF_INVERT_COLOR) != null) InvertBg = Boolean.parseBoolean(properties.getProperty(PREF_INVERT_COLOR)); if (properties.getProperty(PREF_GLOBAL_PWD) != null) Global_pwd = Boolean.parseBoolean(properties.getProperty(PREF_GLOBAL_PWD)); if (properties.getProperty(PREF_SHOW_PAT) != null) ShowPat = Boolean.parseBoolean(properties.getProperty(PREF_SHOW_PAT)); if (properties.getProperty(PREF_SHOW_PWD) != null) ShowPwd = Boolean.parseBoolean(properties.getProperty(PREF_SHOW_PWD)); if (properties.getProperty(PREF_TEXT_SCALE) != null) TextScale = Float.parseFloat(properties.getProperty(PREF_TEXT_SCALE)); if (properties.getProperty(PREF_DEFAULT_PWD) != null) DefaultPwd = properties.getProperty(PREF_DEFAULT_PWD); is.close(); } catch (FileNotFoundException e) { return false; } catch (InvalidPropertiesFormatException e) { Log.e(TAG, "load preferences failed", e); } catch (IOException e) { Log.e(TAG, "load preferences failed", e); } return true; }
@Test public void testSave() { Gson gson = new GsonBuilder().create(); String gameJson = gson.toJson(mGame); // Save the game mSavedGameManager.save(mGame); FileInputStream in = null; try { in = mActivity.openFileInput(mSavedGameManager.getFilename()); } catch (FileNotFoundException e) { Log.d(TAG, "No game has been previously saved."); return; } try { assertEquals("Saved game is not the same.", gameJson, IOUtils.toString(in, "UTF-8")); } catch (IOException e) { Log.e(TAG, "File could not be read.", e); return; } }