Essential Android FileIO edit forum

Working with big files

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> {

    @Override
    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)

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

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

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

    @Override
    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.


Table Of Contents
39 ACRA
64 Menu
112 Loader
119 Xposed
132 Colors
135 Fresco
140 AdMob
147 Button
156 Vk SDK
170 XMPP
176 OpenCV
200 FileIO
203 Moshi
217 Paint
231 AIDL
241 JCodec
243 Okio
255 Looper
  ↑ ↓ to navigate     ↵ to select     Esc to close