Question

I am developing an app that reads it's data from several .json files on the device (or perhaps dropbox, or drive)

The user selects these files using a ACTION_GET_CONTENT intent. This intent returns the URIs of the selected files, from which I obtain the paths, which are sent on to an AsyncTask to load in the background.

prior to android 4.4, this worked fine. Now, certain content choosers (notably android's own "downloads" activity) return uri's that do not contain the actual filename or path.

Unfortunately AsyncTask forces you to override doInBackground(String... filenames) - which takes a series of strings as filenames. I could probably serialize the uri's somehow and pass them in the filenames parameter to the method, but now I'm thinking that obviously wasn't the intent.

Therefore it is prompting me to ask the question: is there a better way to do this?

What is the correct way to allow a user to choose and load a series of files in KitKat

Was it helpful?

Solution

Instead of using AsyncTask<String, Object, Object> you can use AsyncTask<URI, Object, Object>. Which will change your doInBackground(String... args) to doInBackground(URI... args). This will require that you use URIs instead of Strings for all file paths.

If you were to use the non-generic AsyncTask class then you must use the doInBackground(Object... args) method (not type safe).

However, using the generic AsyncTask<Params, Progress, Result> class allows you to specify the type parameters. Params correlates to the varargs Type in doInBackground(Params... args), Progress correlates to the varargs type in protected void onProgressUpdate(Progress... args) and Result correlates to the return type of protected Result doInBackground as well as the parameter type of protected void onPostExecute(Result arg).

In this case, it is best to use generic types as they are free of runtime errors (but not compile time). The same cannot be said for the non-generic AsyncTask that passes type Object references around.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top