Example usage for android.app Activity openFileOutput

List of usage examples for android.app Activity openFileOutput

Introduction

In this page you can find the example usage for android.app Activity openFileOutput.

Prototype

@Override
    public FileOutputStream openFileOutput(String name, int mode) throws FileNotFoundException 

Source Link

Usage

From source file:Main.java

public static String saveToPrivateFile(Activity activity, String filename, Bitmap bmp) {
    FileOutputStream fos;/*from   ww  w.  j a v  a 2s. co  m*/
    try {
        fos = activity.openFileOutput(filename, Context.MODE_PRIVATE);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();

        return filename;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return "";
    }

}

From source file:Main.java

private static void saveActivity(Activity activity, Class<? extends Activity> clazz) {
    FileOutputStream fos = null;//ww  w. j a v  a  2s  . co m
    try {
        fos = activity.openFileOutput(FILENAME, Activity.MODE_PRIVATE);
        fos.write(clazz.getName().getBytes());
    } catch (Exception e) {
        Toast.makeText(activity, "saveActivity: " + e, Toast.LENGTH_LONG).show();
    } finally {
        if (null != fos)
            try {
                fos.close();
            } catch (Exception e) {
            }
    }
}

From source file:Main.java

public static boolean saveScreenshot(Activity activity, String fileName, Bitmap screenshot, boolean sdcard) {
    try {/*from  ww w .  j  a va2s .  co  m*/
        FileOutputStream fos = null;
        if (!sdcard) {
            fos = activity.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        } else {
            File f = new File(fileName);
            f.createNewFile();
            fos = new FileOutputStream(f);
        }
        screenshot.compress(Bitmap.CompressFormat.JPEG, 70, fos);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static boolean savePngScreenshot(Activity activity, String fileName, Bitmap screenshot, boolean sdcard) {
    try {/*from   w w  w  . j  ava  2s  .  c  om*/
        FileOutputStream fos = null;
        if (!sdcard) {
            fos = activity.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        } else {
            File f = new File(fileName);
            f.createNewFile();
            fos = new FileOutputStream(f);
        }
        screenshot.compress(Bitmap.CompressFormat.PNG, 70, fos);
        fos.flush();
        fos.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

public static void saveJsonToFile(String toSave, String filename, Activity currActivity) {

    // check if the file exists
    File dir = currActivity.getFilesDir();
    File file = new File(dir, filename);

    if (file.exists()) {
        file.delete();//www.j  a  v a  2s. c o  m
    }

    FileOutputStream outputStream;
    try {
        outputStream = currActivity.openFileOutput(filename, Context.MODE_APPEND);
        outputStream.write(toSave.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.deviceconnect.android.deviceplugin.pebble.setting.PebbleSettingActivity.java

/**
 * ? pbw ?????? uri ?.//  w w  w  .ja  v a  2 s  . c  o m
 * @return uri ?.
 */
@SuppressLint("WorldReadableFiles")
@SuppressWarnings("deprecation")
private static Uri getPbwFileName(final Activity activity) {
    File file = activity.getFileStreamPath("dc_pebble.pbw");
    try {
        fileCopy(activity.getResources().openRawResource(R.raw.dc_pebble),
                activity.openFileOutput(file.getName(), MODE_WORLD_READABLE));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Uri.fromFile(file);
}

From source file:Main.java

public static void saveAccount(Activity act, String account, String psw) {
    String str = account + "," + psw;
    Properties localProperties = new Properties();
    localProperties.put("account", str);
    try {/*from www .  j a va 2  s  .  c  om*/
        File file = new File(act.getFilesDir() + "/accout.cfg");
        if (!file.exists())
            file.createNewFile();
        FileOutputStream localFileOutputStream = act.openFileOutput("account.cfg", Context.MODE_PRIVATE);
        localProperties.store(localFileOutputStream, "");
        localFileOutputStream.close();
    } catch (Exception localException) {
        localException.printStackTrace();
    }
}

From source file:org.protocoderrunner.utils.FileIO.java

/**
 * Write the data to the file indicate by fileName. The file is created if
 * it doesn't exist.//from w ww  .  j av  a 2 s. c  o  m
 */
public static void write(Activity activity, String data, String fileName) throws IOException {
    FileOutputStream fo = activity.openFileOutput(fileName, 0);
    BufferedWriter bf = new BufferedWriter(new FileWriter(fo.getFD()));
    bf.write(data);
    bf.flush();
    bf.close();
}

From source file:dynamite.zafroshops.app.fragment.AllZopsFragment.java

private void setZops(boolean force) {
    Activity activity = getActivity();
    adapter = new AllZopsGridViewAdapter(activity, R.id.gridItem, types);

    final SharedPreferences preferences = activity.getPreferences(0);
    final SharedPreferences.Editor editor = preferences.edit();

    if (!preferences.contains(StorageKeys.ZOPCATEGORY_KEY)) {
        InputStream is = getResources().openRawResource(R.raw.zops);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        HashMap<String, MobileZop> temp = new HashMap<>();

        types = new ArrayList<>(
                (ArrayList<MobileZop>) new Gson().fromJson(reader, new TypeToken<ArrayList<MobileZop>>() {
                }.getType()));//w ww.java 2 s .  co m
        for (MobileZop type : types) {
            String key = type.Type.toString();

            if (!temp.containsKey(key)) {
                temp.put(key, type);
            }
        }
        types = new ArrayList<>(temp.values());
        if (adapter != null) {
            adapter.setObjects(types);
            adapter.notifyDataSetChanged();
        }

        try {
            FileOutputStream fos = activity.openFileOutput(StorageKeys.ZOPCATEGORY_KEY, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(types);
            oos.close();
            fos.close();
            editor.putString(StorageKeys.ZOPCATEGORY_KEY, Integer.toString(types.size()));
            editor.commit();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        if (preferences.contains(StorageKeys.ZOPCATEGORY_KEY)) {
            try {
                FileInputStream fis = activity.openFileInput(StorageKeys.ZOPCATEGORY_KEY);
                ObjectInputStream ois = new ObjectInputStream(fis);

                types = (ArrayList) ois.readObject();
                ois.close();
                fis.close();

                if (adapter != null) {
                    adapter.setObjects(types);
                    adapter.notifyDataSetChanged();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        ListenableFuture<JsonElement> result = MainActivity.MobileClient.invokeApi("mobileZop", "GET",
                new ArrayList<Pair<String, String>>() {
                    {
                        add(new Pair<String, String>("count", "true"));
                    }
                });

        Futures.addCallback(result, new FutureCallback<JsonElement>() {

            @Override
            public void onSuccess(JsonElement result) {
                Activity activity = getActivity();
                JsonArray typesAsJson = result.getAsJsonArray();
                if (typesAsJson != null) {
                    types = new Gson().fromJson(result, new TypeToken<ArrayList<MobileZop>>() {
                    }.getType());
                    try {
                        FileOutputStream fos = activity.openFileOutput(StorageKeys.ZOPCATEGORY_KEY,
                                Context.MODE_PRIVATE);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);

                        oos.writeObject(types);
                        oos.close();
                        fos.close();
                        editor.putString(StorageKeys.ZOPCATEGORY_KEY, Integer.toString(types.size()));
                        editor.commit();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (adapter != null) {
                    adapter.setObjects(types);
                    GridView zops = (GridView) activity.findViewById(R.id.gridViewZops);
                    RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                    loader.setVisibility(View.INVISIBLE);
                    zops.setVisibility(View.VISIBLE);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                Activity activity = getActivity();
                if (activity != null && types.size() == 0) {
                    GridView zops = (GridView) activity.findViewById(R.id.gridViewZops);
                    RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                    zops.setVisibility(View.INVISIBLE);
                    loader.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}

From source file:dynamite.zafroshops.app.fragment.TypedZopsFragment.java

private void setZops(final boolean force) {
    Activity activity = getActivity();
    adapter = new TypedZopListViewAdapter(getActivity(), R.id.listViewItem, typedZops);
    final SharedPreferences preferences = activity.getPreferences(0);

    final SharedPreferences.Editor editor = preferences.edit();
    final ZopType type = (ZopType) getArguments().get(ARG_ZOP_TYPE);
    final String key = StorageKeys.ZOPS_KEY + type.toString();

    zopType = type;/*from   ww  w.  j a va2s . com*/

    if (!preferences.contains(key)) {
        InputStream is = getResources().openRawResource(R.raw.zops);
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));

        typedZops = new ArrayList<>(Collections2.filter(
                (ArrayList<MobileZop>) new Gson().fromJson(reader, new TypeToken<ArrayList<MobileZop>>() {
                }.getType()), new Predicate<MobileZop>() {
                    @Override
                    public boolean apply(MobileZop input) {
                        return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                                || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                                || input.CountryID.equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                    }
                }));

        try {
            FileOutputStream fos = activity.openFileOutput(key, Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            int count = typedZops.size();

            oos.writeObject(typedZops);
            oos.close();
            fos.close();
            editor.putString(key, Integer.toString(count));
            editor.commit();
            setCount(count);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    else if (preferences.contains(key)) {
        try {
            FileInputStream fis = activity.openFileInput(key);
            ObjectInputStream ois = new ObjectInputStream(fis);

            ArrayList<MobileZop> temp = (ArrayList) ois.readObject();
            typedZops = new ArrayList<>(Collections2.filter(temp, new Predicate<MobileZop>() {
                @Override
                public boolean apply(MobileZop input) {
                    return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                            || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                            || input.CountryID.equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                }
            }));
            ois.close();
            fis.close();
            setCount(typedZops.size());
            if (adapter != null) {
                adapter.setObjects(typedZops);
                adapter.notifyDataSetChanged();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    if (force) {
        final ArrayList<Pair<String, String>> parameters;
        if (MainActivity.LastLocation != null) {
            parameters = new ArrayList<Pair<String, String>>() {
                {
                    add(new Pair<>("latitude", Double.toString(MainActivity.LastLocation.Latitude)));
                    add(new Pair<>("longitude", Double.toString(MainActivity.LastLocation.Longitude)));
                    add(new Pair<>("type", type.getText()));
                }
            };
        } else {
            parameters = new ArrayList<Pair<String, String>>() {
                {
                    add(new Pair<>("type", type.getText()));
                }
            };
        }

        ListenableFuture<JsonElement> result = MainActivity.MobileClient.invokeApi("mobileZop", "GET",
                parameters);

        Futures.addCallback(result, new FutureCallback<JsonElement>() {
            Activity activity = getActivity();

            @Override
            public void onSuccess(JsonElement result) {
                JsonArray typesAsJson = result.getAsJsonArray();
                if (typesAsJson != null) {
                    ArrayList<MobileZop> temp = new Gson().fromJson(result,
                            new TypeToken<ArrayList<MobileZop>>() {
                            }.getType());

                    int max = Collections.max(temp, new Comparator<MobileZop>() {
                        @Override
                        public int compare(MobileZop lhs, MobileZop rhs) {
                            if (lhs.DataVersion < rhs.DataVersion) {
                                return -1;
                            } else if (lhs.DataVersion > rhs.DataVersion) {
                                return 1;
                            } else {
                                return 0;
                            }
                        }
                    }).DataVersion;
                    ((MainActivity) activity).Versions.put(zopType, max);

                    try {
                        FileOutputStream fos = activity.openFileOutput(key, Context.MODE_PRIVATE);
                        ObjectOutputStream oos = new ObjectOutputStream(fos);
                        int count = typedZops.size();

                        typedZops = new ArrayList<>(Collections2.filter(temp, new Predicate<MobileZop>() {
                            @Override
                            public boolean apply(MobileZop input) {
                                return input.Type == type && (!preferences.contains(StorageKeys.COUNTRY_KEY)
                                        || preferences.getString(StorageKeys.COUNTRY_KEY, "").equals("")
                                        || input.CountryID
                                                .equals(preferences.getString(StorageKeys.COUNTRY_KEY, "")));
                            }
                        }));
                        oos.writeObject(typedZops);
                        oos.close();
                        fos.close();
                        editor.putString(key, Integer.toString(count));
                        editor.commit();
                        setCount(count);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (adapter != null) {
                    adapter.setObjects(typedZops);
                    resetVisibility(false);
                    adapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(@NonNull Throwable t) {
                ListView zops = (ListView) activity.findViewById(R.id.listViewZops);
                RelativeLayout loader = (RelativeLayout) activity.findViewById(R.id.relativeLayoutLoader);
                LinearLayout noZops = (LinearLayout) activity.findViewById(R.id.noZops);

                resetVisibility(zops, noZops, loader, false);
            }
        });
    }
}