Working with big files

suggest change

Small files are processed in a fraction of second and you can read / write them in place of the code where you need this. However if the file is bigger or otherwise slower to process, you may need to use AsyncTask in Android to work with the file in the background:

class FileOperation extends AsyncTask<String, Void, File> {

    protected File doInBackground(String... params) {
      try {
        File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_DOCUMENTS), "bigAndComplexDocument.odf");
        FileOutputStream out = new FileOutputStream(file)

       ... (write the document)

        return file;
        } catch (IOException ex) {
           Log.e("Unable to write", ex);
           return null;

    protected void onPostExecute(File result) {
      // This is called when we finish 

    protected void onPreExecute() {
       // This is called before we begin

    protected void onProgressUpdate(Void... values) {
        // Unlikely required for this example

and then

new FileOperation().execute("Some parameters");

This SO question contains the complete example on how to create and call the AsyncTask. Also see the question on error handling on how to handle IOExceptions and other errors.

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents