Android : Write app directory without worrying anything

TL;DR : this

Almost all of us who has developed in android for decent amount of time has faced a situation where we have to export a data of our application to a file in sd-card (or better say technically external storage). And I bet too many people faced the challenge to make this thing working in every device. Sometimes (y)our app is writing a file where there is no external storage, some times we use hard-coded path, because at some point of history, sd cards were mounted on that path and all devices follow this ritual. At some point of time the external storage is not available for us to write a file because of various reasons. And after crossing all these hurdles, we want to save all our app-created files in directory named after our app. For better organization and/or for any kind of branding purpose.

Many of us faced this problem, many of us are facing this right now. I have faced this issue too. This is a general operation that we have done n times and will do n+n times. What if there is a kind of plugin or library developed for this, That automates all the process, without worrying little details and with giving us proper errors whenever we face?

That’s where AppExternalFileWriter comes into the picture. I am talking about a class I have recently developed that (I believe) mostly automates the process said above. Doing some things automatically, as well let us(the programmers) do some extra cherry-picking at will.

Interested? Want to use? If not, you are not forced to read anything after this line. If yes, these are the droids you are looking for.

First of all, let me tell you how to work with the class.

1. Create an object of AppExternalFileWriter, to do that you need a Context object to pass to.

2. If you want to create a subfolder use suitable createSubDirectory variants.

3. use writeDataToFile or writeDataToTimeStampedFile variants as per your wish.

4. If you want to write a data where a file name should be a time stamp use writeDataToTimeStampedFile variants.

5. Apart from all I nearly forgot to add that, If anything is wrong with external storage, like storage not mounted, corrupt,  shared as mass storage, not enough space available, or even trying to create a library already created. The class will throw ExternalFileWriterException with the message stating what happened. Thus giving you the power to handle some crappy situations.

Here are some important variants you may want to use.

  1. writeDataToFile – Without parent directories
    writeDataToFile(String fileName, byte[] data);
    writeDataToFile(String fileName, String data);

    Writes data to desired file in Application directory.

  2. writeDataToFile – With parent directories
    writeDataToFile(File parent, String fileName, byte[] data);
    writeDataToFile(File parent, String fileName, String data);

    Writes data to desired file in other directory.

  3. writeDataToTimeStampedFile variants – Without parent directories
    writeDataToTimeStampedFile(String extension, byte[] data)
    writeDataToTimeStampedFile(String extension, String data)

    Writes data to desired file with timestamp with extension in Application directory.

  4. writeDataToTimeStampedFile variants – With parent directories
    writeDataToTimeStampedFile(String extension, byte[] data)
    writeDataToTimeStampedFile(String extension, String data)

    Writes data to desired file with timestamp with extension in other directory.

  5. createSubDirectory variants
    createSubDirectory(File parent, String directoryName)

    Creates subdirectory in application directory

    createSubDirectory(String directoryName)

    Creates subdirectory in any other directory

some other useful methods

  1. getAppDirectory()

    get File object of created app directory

  2. getExternalStorageDirectory()

    get File object of external storage directory

The full code with Sample is shared at this repository, fork and use it at your will.

May the force be with you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s