0

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

Brent
  • 16,259
  • 12
  • 42
  • 42
  • 1
    Why do you have to use `doInBackground(String...)`? You could use `AsyncTask` which would require you to use `doInBackground(URI...)`. You could use URIs instead of strings for all file paths. – iamreptar Jan 03 '14 at 21:25
  • @iamreptar - I didn't know that. That could be my answer right there! – Brent Jan 03 '14 at 23:10
  • Great! I've submitted the comment as a solution with extra detail. – iamreptar Jan 04 '14 at 01:45

1 Answers1

1

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.

iamreptar
  • 1,461
  • 16
  • 29
  • This had potential, but in the end I was not able to get it working. Not sure if it is the "correct" answer, but it definitely taught me how this works better. – Brent Jan 04 '14 at 19:38