I have the queryAppIcon()
method that queries and stores images in the array appIconDrawable. However, this task is taking forever (i.e. click button to view images, but I get a 5 second pause on my app before the new screen appears) so I decided to use an AsyncTask
. However, I'm calling a method with no parameters, so I can't pass in any information (i.e. first ParseResult.get(0).get("Icon")
, then ParseResult.get(1).get("appIcon"), ..)
. Also, no information is being passed back here as my appIconDrawable[i]
doesn't equal anything since I don't know how the processFinish method would pass back information to the array. Am I setting it up right? What should I be doing? Thanks
// global vars
final Drawable[] appIconDrawable = null;
int i;
public Drawable[] queryAppIcon() throws ParseException, IOException {
ParseQuery<ParseObject> query = ParseQuery.getQuery("AndroidStoreContent");
query.whereExists("appIcon");
List<ParseObject> ParseResult = query.find();
// initialize Drawable array
final Drawable[] appIconDrawable = new Drawable[ParseResult.size()];
for (i = 0; i < ParseResult.size(); i++) {
ParseFile pf = (ParseFile) ParseResult.get(i).get("appIcon");
startDownload(pf);
}
return appIconDrawable;
}
public void startDownload(ParseFile pf) {
new DownloadImageTask(this).execute(pf);
}
public class DownloadImageTask extends AsyncTask<ParseFile, Void, Drawable> {
private AsyncResponse ar;
DownloadImageTask(AsyncResponse ar) {
this.ar = ar;
}
@Override
protected Drawable doInBackground(ParseFile... pf) {
return fetchDrawable(pf[0]);
}
protected void onPostExecute(Drawable result) {
ar.processFinish(result);
}
public Drawable fetchDrawable(ParseFile pf) {
InputStream is;
try {
is = (InputStream) new URL(pf.getUrl()).getContent();
return Drawable.createFromStream(is,null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public void processFinish(Drawable d) {
appIconDrawable[i] = d; // i also tried testing appIconDrawable[1] = d and the app loaded with all blank images and then crashes
}
This is the interface, AsyncResponse
:
public interface AsyncResponse {
void processFinish(Drawable d);
}