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

private static void writeEntitytoFile(HttpEntity entity, Context context)
        throws IllegalStateException, IOException {
    InputStream is = entity.getContent();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    BufferedWriter bw = new BufferedWriter(
            new OutputStreamWriter(context.openFileOutput("TEMP.html", Context.MODE_PRIVATE)));
    String line;/*w w w . j a  v  a 2  s  . c om*/
    while ((line = br.readLine()) != null) {
        bw.write(line);
    }
}

From source file:es.deustotech.piramide.utils.tts.TextToSpeechWeb.java

private static synchronized void speech(final Context context, final String text, final String language) {
    executor.submit(new Runnable() {
        @Override/*from w w w .jav a 2s .co  m*/
        public void run() {
            try {
                final String encodedUrl = Constants.URL + language + "&q="
                        + URLEncoder.encode(text, Encoding.UTF_8.name());
                final DefaultHttpClient client = new DefaultHttpClient();
                HttpParams params = new BasicHttpParams();
                params.setParameter("http.protocol.content-charset", "UTF-8");
                client.setParams(params);
                final FileOutputStream fos = context.openFileOutput(Constants.MP3_FILE,
                        Context.MODE_WORLD_READABLE);
                try {
                    try {
                        final HttpResponse response = client.execute(new HttpGet(encodedUrl));
                        downloadFile(response, fos);
                    } finally {
                        fos.close();
                    }
                    final String filePath = context.getFilesDir().getAbsolutePath() + "/" + Constants.MP3_FILE;
                    final MediaPlayer player = MediaPlayer.create(context.getApplicationContext(),
                            Uri.fromFile(new File(filePath)));
                    player.start();
                    Thread.sleep(player.getDuration());
                    while (player.isPlaying()) {
                        Thread.sleep(100);
                    }
                    player.stop();

                } finally {
                    context.deleteFile(Constants.MP3_FILE);
                }
            } catch (InterruptedException ie) {
                // ok
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

From source file:com.brewcrewfoo.performance.util.Helpers.java

public synchronized static void get_assetsBinary(String fn, Context c) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {//w  ww.  ja  v  a 2s  .  c om
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(buffer);
            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:net.vexelon.bgrates.Utils.java

/**
 * Move a file stored in the cache to the internal storage of the specified context
 * @param context/* ww  w .  j  av a 2  s  . c  om*/
 * @param cacheFile
 * @param internalStorageName
 */
public static boolean moveCacheFile(Context context, File cacheFile, String internalStorageName) {

    boolean ret = false;
    FileInputStream fis = null;
    FileOutputStream fos = null;

    try {
        fis = new FileInputStream(cacheFile);

        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[1024];
        int read = -1;
        while ((read = fis.read(buffer)) != -1) {
            baos.write(buffer, 0, read);
        }
        baos.close();
        fis.close();

        fos = context.openFileOutput(internalStorageName, Context.MODE_PRIVATE);
        baos.writeTo(fos);
        fos.close();

        // delete cache
        cacheFile.delete();

        ret = true;
    } catch (Exception e) {
        //Log.e(TAG, "Error saving previous rates!");
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException e) {
        }
        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
        }
    }

    return ret;
}

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

private static TagNode TagNodeFromURLHelper(InputStream is, String fullTag, Handler handler, Context context,
        HtmlCleaner cleaner) throws IOException {
    SharedPreferences settings = context.getSharedPreferences(Settings.SETTINGS_FILE_NAME, 0);
    boolean disableSmartParsing = settings.getBoolean(Settings.DISABLE_SMART_PARSING, false);
    if (fullTag != null && !disableSmartParsing) {
        FileOutputStream fos = context.openFileOutput(TEMP_FILE_NAME, Context.MODE_WORLD_WRITEABLE);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        TagParser.extractTagToFile(fullTag, is, bw);
        bw.flush();//from w w w .j  a  va2s  .com
        bw.close();
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);

        return cleaner.clean(context.openFileInput(TEMP_FILE_NAME));
    } else {
        if (handler != null)
            handler.sendEmptyMessage(PROGRESS_PARSING);
        return cleaner.clean(is);
    }
}

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

public static void get_assetsBinary(String fn, Context c) {
    byte[] buffer;
    final AssetManager assetManager = c.getAssets();
    try {/*from   w  w  w  .  j  av a 2 s  .c o  m*/
        InputStream f = assetManager.open(fn);
        buffer = new byte[f.available()];
        f.read(buffer);
        f.close();
        try {
            FileOutputStream fos;
            fos = c.openFileOutput(fn, Context.MODE_PRIVATE);
            fos.write(buffer);
            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:joshuatee.wx.UtilityFTP.java

public static void GetNids(Context c, String url, String path) {

    try {// w  ww  . j a  v a  2 s .c  om
        FTPClient ftp = new FTPClient();

        //String url = "tgftp.nws.noaa.gov";
        //String user = "ftp";
        //String pass = "anonymous";

        ftp.connect(url);

        if (!ftp.login("ftp", "anonymous")) {
            ftp.logout();
        }

        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
        }

        ftp.enterLocalPassiveMode();
        ftp.changeWorkingDirectory(path);
        //reply = ftp.getReplyCode();
        //String fn = "sn.last";

        FileOutputStream fos = c.openFileOutput("nids", Context.MODE_PRIVATE);
        ftp.retrieveFile("sn.last", fos);
        //reply = ftp.getReplyCode();
        fos.close();

        ftp.logout();
        ftp.disconnect();

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

From source file:org.matrix.matrixandroidsdk.db.ConsoleMediasCache.java

/**
 * Save a media to the local cache//w  w w  .j a va 2s . c om
 * it could be used for unsent media to allow them to be resent.
 * @param stream the file stream to save
 * @param defaultFileName the filename is provided, if null, a filename will be generated
 * @return the media cache URL
 */
public static String saveMedia(InputStream stream, Context context, String defaultFileName) {
    String filename = (defaultFileName == null) ? ("file" + System.currentTimeMillis()) : defaultFileName;
    String cacheURL = null;

    try {
        FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE);

        try {
            byte[] buf = new byte[1024 * 32];

            int len;
            while ((len = stream.read(buf)) != -1) {
                fos.write(buf, 0, len);
            }
        } catch (Exception e) {
        }

        fos.flush();
        fos.close();
        stream.close();

        cacheURL = Uri.fromFile(context.getFileStreamPath(filename)).toString();
    } catch (Exception e) {

    }

    return cacheURL;
}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

private static boolean copyDefaultMap(Context context) {
    try {/*from   w  ww.j  a  v  a2  s  .c om*/
        // write file to sd for mapsforge
        OutputStream mapFileWriter = new BufferedOutputStream(
                context.openFileOutput(MapFragment.MAP_FILE_NAME, Context.MODE_PRIVATE));
        InputStream assetData = new BufferedInputStream(context.getAssets().open(MapFragment.MAP_FILE_NAME));

        byte[] buffer = new byte[1024];
        int len = assetData.read(buffer);
        while (len != -1) {
            mapFileWriter.write(buffer, 0, len);
            len = assetData.read(buffer);
        }
        mapFileWriter.close();
    } catch (FileNotFoundException e) {
        Log.e(TAG, "copyDefaultMap", e);
        return false;
    } catch (IOException e) {
        Log.e(TAG, "copyDefaultMap", e);
        return false;
    }
    return true;
}

From source file:org.voidsink.anewjkuapp.utils.AppUtils.java

private static boolean importDefaultPois(Context context) {
    // import JKU Pois
    try {//from w w  w .j a v  a  2 s  .  c o  m
        // write file to sd for import
        OutputStream mapFileWriter = new BufferedOutputStream(
                context.openFileOutput(DEFAULT_POI_FILE_NAME, Context.MODE_PRIVATE));
        InputStream assetData = new BufferedInputStream(context.getAssets().open(DEFAULT_POI_FILE_NAME));

        byte[] buffer = new byte[1024];
        int len = assetData.read(buffer);
        while (len != -1) {
            mapFileWriter.write(buffer, 0, len);
            len = assetData.read(buffer);
        }
        mapFileWriter.close();

        // import file
        return executeEm(context, new Callable<?>[] {
                new ImportPoiTask(context, new File(context.getFilesDir(), DEFAULT_POI_FILE_NAME), true) },
                false);
    } catch (IOException e) {
        Analytics.sendException(context, e, false);
        return false;
    }
}