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.cerema.cloud2.lib.common.network.NetworkUtils.java

public static void addCertToKnownServersStore(Certificate cert, Context context)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {

    KeyStore knownServers = getKnownServersStore(context);
    knownServers.setCertificateEntry(Integer.toString(cert.hashCode()), cert);
    FileOutputStream fos = null;// w  w  w  .j av a2 s.  c  o  m
    try {
        fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
        knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
    } finally {
        fos.close();
    }
}

From source file:com.owncloud.android.lib.common.network.NetworkUtils.java

public static void removeCertFromKnownServersStore(String alias, Context context)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {

    KeyStore knownServers = getKnownServersStore(context);
    if (knownServers.containsAlias(alias)) {
        knownServers.deleteEntry(alias);
    }/*from  w  w w. j a  v  a 2s .  c om*/
    FileOutputStream fos = null;
    try {
        fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
        knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
    } finally {
        fos.close();
    }
}

From source file:com.owncloud.android.lib.common.network.NetworkUtils.java

public static String addCertToKnownServersStore(Certificate cert, Context context)
        throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {

    KeyStore knownServers = getKnownServersStore(context);
    String alias = Integer.toString(cert.hashCode());
    knownServers.setCertificateEntry(alias, cert);
    FileOutputStream fos = null;//  w  ww.j  a  v a 2s .c  om
    try {
        fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
        knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
    } finally {
        fos.close();
    }
    return alias;
}

From source file:com.ibm.pi.beacon.PIBeaconSensor.java

private static void savePIAPIAdapter(Context context, PIAPIAdapter adapter) {
    ObjectOutputStream adapterStream = null;
    try {/* w ww.  java  2 s  .  c  o m*/
        adapterStream = new ObjectOutputStream(
                context.openFileOutput("piapiadapter.data", Context.MODE_PRIVATE));
        adapterStream.writeObject(adapter);
        adapterStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (adapterStream != null) {
            try {
                adapterStream.flush();
                adapterStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

/**
 * Show a level/*from w  w  w.  jav  a 2s . com*/
 * 
 * @param context The context
 * @param level The level
 */
public static void showLevel(Context context, int level) {
    String filename;
    switch (level) {
    case 1: {
        filename = "ground_floor.png";
        break;
    }
    case 2: {
        filename = "talks_floor.png";
        break;
    }

    default: {
        return;
    }
    }
    File f = new File(context.getFilesDir() + "/" + filename);
    try {
        if (f.exists() == false) {
            InputStream is = context.getAssets().open(filename);
            FileOutputStream fos = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
            byte[] buffer = new byte[is.available()];
            is.read(buffer);
            // write the stream to file
            fos.write(buffer, 0, buffer.length);
            fos.close();
            is.close();
        }

        // Prepare the intent
        //TODO - create an activity for this instead. Internal viewers might be quite heavy
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(f), "image/png");
        context.startActivity(intent);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

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

public static void saveToFile(String filename, Context ctx, String text) {
    FileOutputStream fos;//from   www  .j  av a 2  s  . c  om
    try {
        fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(text.getBytes());
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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

public static void WriteBtn(Context ctx, String file, String text) {
    // add-write text into file
    try {/*from www  .ja v a2  s.  c  om*/
        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.entertailion.android.slideshow.utils.Utils.java

/**
 * Save a bitmap to a local file.//from w  w w  .j a v  a 2s.c o  m
 * 
 * @param context
 * @param bitmap
 * @param targetWidth
 * @param targetHeight
 * @param fileName
 * @throws IOException
 */
public static final void saveToFile(Context context, Bitmap bitmap, int targetWidth, int targetHeight,
        String fileName) throws IOException {
    FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
    // FileOutputStream fos = new FileOutputStream(fileName);
    if (bitmap.getWidth() == targetWidth && bitmap.getHeight() == targetHeight) {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    } else {
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, false);
        scaledBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
    }
    fos.close();
}

From source file:it.mb.whatshare.PairOutboundActivity.java

/**
 * Saves the argument <tt>device</tt> as the (only) configured outbound
 * device./*from ww w  . ja  v  a 2 s.  co m*/
 * 
 * <p>
 * If <tt>device</tt> is <code>null</code>, the currently configured device
 * is deleted.
 * 
 * @param device
 *            the device to be stored, or <code>null</code> if the current
 *            association must be discarded
 * @param context
 *            the application's context (used to open the association file
 *            with)
 * @throws IOException
 *             in case something is wrong with the file
 * @throws JSONException
 *             in case something is wrong with the argument <tt>device</tt>
 */
public static void savePairing(PairedDevice device, Context context) throws IOException, JSONException {
    if (device == null) {
        Utils.debug("deleting outbound device... %s",
                context.deleteFile(PAIRING_FILE_NAME) ? "success" : "fail");
    } else {
        FileOutputStream fos = context.openFileOutput(PAIRING_FILE_NAME, Context.MODE_PRIVATE);
        // @formatter:off
        JSONObject json = new JSONObject().put("name", device.name).put("type", device.type).put("assignedID",
                device.id);
        // @formatter:on
        PrintStream writer = new PrintStream(fos);
        writer.append(json.toString());
        writer.flush();
        writer.close();
    }
}

From source file:com.xiaomi.account.utils.SysHelper.java

public static File saveAsFile(Context context, InputStream imgStream, String filename) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(imgStream);
    OutputStream os = new BufferedOutputStream(context.openFileOutput(filename, 0));
    byte[] buffer = new byte[1024];
    while (true) {
        try {//from ww w.  j a  v  a  2  s  .c o  m
            int count = bis.read(buffer);
            if (count == -1) {
                break;
            }
            os.write(buffer, 0, count);
        } finally {
            try {
                os.flush();
            } catch (IOException e) {
            }
            try {
                os.close();
            } catch (IOException e2) {
            }
        }
    }
    try {
        os.close();
    } catch (IOException e3) {
    }
    return context.getFileStreamPath(filename);
}