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:Main.java

public static void copyToMemory(Context context, String srcFilePath, String dictFileName) throws IOException {
    File srcFile = new File(srcFilePath);
    if (!srcFile.exists() || srcFile.isDirectory()) {
        return;//from  w  ww .  jav a  2s.  c  om
    }
    BufferedInputStream inBufferedInputStream = new BufferedInputStream(new FileInputStream(srcFile));
    FileOutputStream fos = context.openFileOutput(dictFileName, 0);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    byte[] b = new byte[1024 * 4];
    int len;
    while ((len = inBufferedInputStream.read(b)) != -1) {
        bos.write(b, 0, len);
        bos.flush();
    }
    inBufferedInputStream.close();
    bos.close();
}

From source file:Main.java

public static Uri getAllContacts(ContentResolver cr, Uri internal, Context context, String timeStamp) {

    String[] contactsArray = new String[2];
    Uri contactsUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

    Cursor cur = cr.query(contactsUri, null, null, null, null);

    FileOutputStream fOut = null;
    try {//from  ww w  . j  ava2  s.c o m
        fOut = context.openFileOutput("contacts_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        contactsArray[0] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
                .toString();
        contactsArray[1] = cur
                .getString(cur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

        writeToOutputStreamArray(contactsArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:ru.kaefik.isaifutdinov.an_weather_widget.utils.Utils.java

public static void saveFile(String filename, String strText, Context context) {
    try {//from   w  w w  .  j  a  v  a 2  s . co m
        FileOutputStream outputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
        OutputStreamWriter osw = new OutputStreamWriter(outputStream);
        osw.write(strText);
        osw.close();
    } catch (Throwable t) {
        //TODO: ?  ??   ,  ?  ?  
    }
}

From source file:at.wada811.dayscounter.CrashExceptionHandler.java

public static void makeReportFile(Context context, Throwable throwable) {
    PrintWriter writer = null;/*from  w  w  w  .j a va  2 s.  c o m*/
    FileOutputStream outputStream;
    try {
        outputStream = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
        writer = new PrintWriter(outputStream);
        String report = CrashExceptionHandler.makeReport(context, throwable);
        writer.print(report);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:com.fusionjack.slimota.configs.LinkConfig.java

public static void persistLinks(List<OTALink> links, Context context) {
    try {//ww  w . j  a va  2s .  co  m
        File dir = context.getFilesDir();
        File file = new File(dir, FILENAME);
        if (file.exists()) {
            file.delete();
        }

        FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);

        JSONArray jsonLinks = new JSONArray();
        for (OTALink link : links) {
            JSONObject jsonLink = new JSONObject();
            jsonLink.put(OTAParser.ID, link.getId());
            jsonLink.put(OTAParser.TITLE, link.getTitle());
            jsonLink.put(OTAParser.DESCRIPTION, link.getDescription());
            jsonLink.put(OTAParser.URL, link.getUrl());
            jsonLinks.put(jsonLink);
        }

        fos.write(jsonLinks.toString().getBytes());
        fos.close();

        LinkConfigListener listener = getLinkConfigListener(context);
        if (listener != null) {
            listener.onConfigChange();
        }
    } catch (IOException | JSONException e) {
        OTAUtils.logError(e);
    }
}

From source file:Main.java

public static Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    //Cursor cur= cr.query(uri, smsLogArray, null ,null,null);
    FileOutputStream fOut = null;

    try {//w  ww  . j a va2 s. c  om
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:Main.java

public static boolean installDll(Context context) {
    boolean isSuccess = false;
    File file = context.getFileStreamPath("Disdll.dll");
    boolean isDeleteSuccess = true;
    if (file.exists()) {
        isDeleteSuccess = file.delete();
    }//from   w  w  w .j  a v a  2s  .  c o m
    if (isDeleteSuccess) {
        try {
            FileOutputStream outputStream = context.openFileOutput("Disdll.dll", Context.MODE_PRIVATE);
            InputStream inputStream = context.getAssets().open("Disdll.dll");
            byte[] temp = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(temp)) != -1) {
                outputStream.write(temp, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        isSuccess = checkIfDllInstalled(context);
    }
    return isSuccess;
}

From source file:com.jamesgiang.aussnowcam.Utils.java

public static void WriteSettings(Context context, String data, String file) throws IOException {
    FileOutputStream fOut = null;
    OutputStreamWriter osw = null;
    fOut = context.openFileOutput(file, Context.MODE_PRIVATE);
    osw = new OutputStreamWriter(fOut);
    osw.write(data);// w w  w .  j  a  va 2s.c o  m
    osw.close();
    fOut.close();
}

From source file:Main.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;/*from  w  ww  . ja v  a2  s .com*/
    try {
        fos = context.openFileOutput(LOCAL_TRUSTSTORE_FILENAME, Context.MODE_PRIVATE);
        knownServers.store(fos, LOCAL_TRUSTSTORE_PASSWORD.toCharArray());
    } finally {
        fos.close();
    }
}

From source file:Main.java

/**
 * Write image to internal storage/* w ww .  ja  v a  2 s .c o  m*/
 * @param context  : Context android.
 * @param fileName : Image file name.
 * @param image    : Bitmap image format PNG only.
 */
public static void saveImageToInternalStorage(Context context, String fileName, Bitmap image) {
    // Convert Bitmap to byteArray
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    // Open fileOutput with fileName and write byteArray
    try {
        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        if (fos != null) {
            fos.write(byteArray);
            fos.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}