Example usage for android.content Context openFileOutput

List of usage examples for android.content Context openFileOutput

Introduction

In this page you can find the example usage for android.content Context openFileOutput.

Prototype

public abstract FileOutputStream openFileOutput(String name, @FileMode int mode) throws FileNotFoundException;

Source Link

Document

Open a private file associated with this Context's application package for writing.

Usage

From source file:com.thoughtmetric.tl.TLLib.java

public static Object[] parseEditText(HtmlCleaner cleaner, URL url, TLHandler handler, Context context)
        throws IOException {
    // Although probably not THE worst hack I've written, this function ranks near the top.
    // TODO: rework this routine get rid of code duplication.

    DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.setCookieStore(cookieStore);

    HttpGet httpGet = new HttpGet(url.toExternalForm());
    HttpResponse response = httpclient.execute(httpGet);

    handler.sendEmptyMessage(PROGRESS_DOWNLOADING);
    InputStream is = response.getEntity().getContent();

    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);

    FileOutputStream fos = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_PRIVATE);
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

    String line;// ww w  . j  a v  a2 s . c o  m
    String formStart = "<form action=\"/forum/edit.php";
    while ((line = br.readLine()) != null) {
        if (line.startsWith(formStart)) {
            Log.d(TAG, line);
            bw.write(line);
            break;
        }
    }

    String start = "\t\t<textarea";
    String end = "\t\t<p>";
    StringBuffer sb = new StringBuffer();
    while ((line = br.readLine()) != null) {
        if (line.startsWith(start)) {
            bw.write("</form>");
            int i = line.lastIndexOf('>');
            sb.append(Html.fromHtml(line.substring(i + 1)).toString());
            sb.append("\n");
            break;
        } else {
            bw.write(line);
        }
    }

    while ((line = br.readLine()) != null) {
        if (line.startsWith(end)) {
            break;
        }
        sb.append(Html.fromHtml(line).toString());
        sb.append("\n");
    }

    bw.flush();
    bw.close();

    if (handler != null)
        handler.sendEmptyMessage(PROGRESS_PARSING);

    Object[] ret = new Object[2];

    ret[0] = sb.toString();
    ret[1] = cleaner.clean(context.openFileInput(TEMP_FILE_NAME));
    return ret;
}

From source file:com.jamessimshaw.wallpaperhelper.datasources.WallpaperFileHelper.java

public void saveWallpaper(Context context, Wallpaper wallpaper) throws IOException {
    String filename = wallpaper.isLandscape() ? LANDSCAPE_FILENAME : PORTRAIT_FILENAME;

    //TODO: Set this to be an async task
    FileOutputStream fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
    wallpaper.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
    fileOutputStream.close();/*  w w  w .  j  a va 2s  .co  m*/
}

From source file:mx.klozz.xperience.tweaker.helpers.Helpers.java

public static void get_assetsScript(String fn, Context c, String prefix, String postfix) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {/*from   w w w. j  a va 2  s  .co m*/
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        final String s = new String(buffer);
        final StringBuilder sb = new StringBuilder(s);
        if (!postfix.equals("")) {
            sb.append("\n\n").append(postfix);
        }
        if (!prefix.equals("")) {
            sb.insert(0, prefix + "\n");
        }
        sb.insert(0, "#!" + Helpers.BinExist("sh") + "\n\n");
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(sb.toString().getBytes());
            fos.close();

        } catch (IOException e) {
            Log.d(TAG, "error write " + fn + " file");
            e.printStackTrace();
        }
    } catch (IOException e) {
        Log.d(TAG, "error read " + fn + " file");
        e.printStackTrace();
    }
}

From source file:com.android.settings.util.Helpers2.java

public static void get_assetsScript(String fn, Context c, String prefix, String postfix) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {//from  w w w. ja  va 2s . c  o  m
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        final String s = new String(buffer);
        final StringBuilder sb = new StringBuilder(s);
        if (!postfix.equals("")) {
            sb.append("\n\n").append(postfix);
        }
        if (!prefix.equals("")) {
            sb.insert(0, prefix + "\n");
        }
        sb.insert(0, "#!" + Helpers2.binExist("sh") + "\n\n");
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(sb.toString().getBytes());
            fos.close();

        } catch (IOException e) {
            Log.d(TAG, "error write " + fn + " file");
            e.printStackTrace();
        }

    } catch (IOException e) {
        Log.d(TAG, "error read " + fn + " file");
        e.printStackTrace();
    }
}

From source file:com.agateau.equiv.ui.Kernel.java

public void saveDay(Context context) {
    NLog.i("");/*from w  ww .ja v a2 s. c  o  m*/
    FileOutputStream stream;
    try {
        stream = context.openFileOutput(DAY_JSON, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        NLog.e("Failed to open %s for writing: %s", DAY_JSON, e);
        return;
    }
    try {
        DayJsonIO.write(stream, mDay);
        stream.flush();
        stream.close();
    } catch (IOException e) {
        NLog.e("Failed to save day: %s", e);
    }
}

From source file:com.agateau.equiv.ui.Kernel.java

public void saveCustomProductList(Context context) {
    NLog.i("");/*from w ww.  ja v  a  2s  .  co  m*/
    FileOutputStream stream;
    try {
        stream = context.openFileOutput(CUSTOM_PRODUCTS_CSV, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        NLog.e("Failed to open %s for writing: %s", CUSTOM_PRODUCTS_CSV, e);
        return;
    }
    try {
        ProductStoreCsvIO.write(stream, mProductStore.getCustomItems());
        stream.flush();
        stream.close();
    } catch (IOException e) {
        NLog.e("Failed to save custom productsto %s: %s", CUSTOM_PRODUCTS_CSV, e);
    }
}

From source file:com.project.qrypto.keymanagement.KeyManager.java

/**
 * Returns a file out stream for a keystore. It will use the settings loaded to determine where it goes.
 * @param context the context to use//from   w w w .  j  a v a  2 s .com
 * @return the opened FileInputStream 
 * 
 * @throws FileNotFoundException if the file is not found
 */
private FileOutputStream getAssociatedOutFileStream(Context context) throws FileNotFoundException {
    if (internalStorage) {
        return context.openFileOutput(KEYSTORE, Context.MODE_PRIVATE);
    } else {
        return null;
    }
}

From source file:org.rapidandroid.ApplicationGlobals.java

/**
 * /* w  w  w. j  a va 2  s .c  o  m*/
 * @param context
 * @param activateLogging
 * @param activateAll
 * @param inProgressReply
 * @param inProgressReplyText
 * @param parseReply
 * @param parseReplyText
 * @param failedReply
 * @param failedReplyText
 */
public static void saveGlobalSettings(Context context, boolean activateLogging, boolean activateAll,
        boolean inProgressReply, String inProgressReplyText, boolean parseReply, String parseReplyText,
        boolean failedReply, String failedReplyText) {
    JSONObject settingsObj = new JSONObject();
    FileOutputStream fos = null;
    try {
        settingsObj.put(KEY_ACTIVE_LOGGING, activateLogging);
        settingsObj.put(KEY_ACTIVE_ALL, activateAll);
        settingsObj.put(KEY_INPROGRESS_REPLY, inProgressReply);
        settingsObj.put(KEY_PARSE_INPROGRESS_TEXT, inProgressReplyText);
        settingsObj.put(KEY_FAILED_REPLY, failedReply);
        settingsObj.put(KEY_PARSE_REPLY, parseReply);
        settingsObj.put(KEY_PARSE_REPLY_TEXT, parseReplyText);
        settingsObj.put(KEY_FAILED_REPLY, failedReply);
        settingsObj.put(KEY_FAILED_REPLY_TEXT, failedReplyText);
    } catch (JSONException e1) {
        e1.printStackTrace();
    }

    try {
        fos = context.openFileOutput(SETTINGS_FILE, Context.MODE_PRIVATE);
        fos.write(settingsObj.toString().getBytes());
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d(LOG_DEBUG_KEY, e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.d(LOG_DEBUG_KEY, e.getMessage());
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        globalsLoaded = false;
        ApplicationGlobals.initGlobals(context);
    }
}

From source file:pt.ubi.di.pdm.swipe.MainActivity.java

public void WriteBtn(Context ctx, String file, String text) {
    // add-write text into file
    try {/* w  w w  .j  av a 2s  .co m*/
        FileOutputStream fileout = ctx.openFileOutput(file, Context.MODE_PRIVATE);
        OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
        outputWriter.write(text);
        outputWriter.close();

        //display file saved message
        //Toast.makeText(ctx, "Ficheiro Gravado!", Toast.LENGTH_SHORT).show();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.rcythr.masq.keymanagement.KeyManager.java

/**
 * Returns a file out stream for a keystore. It will use the settings loaded to determine where it goes.
 * @param context the context to use/*from   w  ww .  j av  a  2s .  c o  m*/
 * @return the opened FileInputStream 
 * 
 * @throws FileNotFoundException if the file is not found
 */
private FileOutputStream getAssociatedOutFileStream(Context context) throws FileNotFoundException {
    if (internalStorage) {
        return context.openFileOutput(KEYSTORE, Context.MODE_PRIVATE);
    } else {
        return new FileOutputStream(getSDCardFile());
    }
}