I'm very new to Android development, but have deduced that I have not overridden the onPostExecute method correctly (error), but I am lost as to where I've gone wrong. Am I right to assume that onPostExecute is called by the main UI thread and not Async? onPostExecute isn't called at all at the moment, and adding @Override has given me the error..
Basically, I'm just downloading an image. Learning this Async business and Java too.
class ImageReceiver extends AsyncTask<String, Integer, Bitmap>{
private View rootView;
public ImageReceiver(View rootView){
this.rootView=rootView;
}
@Override
protected Bitmap doInBackground(String... params) {
System.out.println("got here");
try {
URL url = new URL(params[0]);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
if(httpCon.getResponseCode() !=200){
throw new Exception("Failed to connect");
}
InputStream is = httpCon.getInputStream();
Bitmap img = BitmapFactory.decodeStream(is);
System.out.println(url.toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void OnPostExecute(Bitmap img){
System.out.println("postexecute");
ImageView im = (ImageView)rootView.findViewById(R.id.incomming_image);
im.setImageBitmap(img);
}
}
I call the new thead like so in the OnCreate method of the MainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
View = getWindow().getDecorView().findViewById(android.R.id.content);
ImageReceiver imageReceiver = new ImageReceiver(View);
imageReceiver.execute("http://joshuacroft.co.uk/images/killzone_mercenary_2.jpg");
}
Unsure where I can correct, so any help is appreciated.
0 comments:
Post a Comment