Below is my Async class
public class GetBitMapFromURL extends AsyncTask<String, Integer, String>
{
byte[] tempByte;
private Bitmap bmap;
@Override
protected String doInBackground(String... params)
{
// TODO Auto-generated method stub
String stringUrl = params[0];
//bmap = null;
try
{
URL url = new URL(stringUrl);
InputStream is = (InputStream) url.getContent();
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = is.read(buffer)) != -1)
{
output.write(buffer, 0, bytesRead);
}
tempByte = output.toByteArray();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return "Success";
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
Bitmap tempBitMap = BitmapFactory.decodeByteArray(tempByte, 0, tempByte.length);
//Log.d("Bitmap bmap value on PostExecute", "bmap="+bmap);
setBitMap(tempBitMap);
//imageView.setImageBitmap(bImg);
}
void setBitMap(Bitmap bitMapSet)
{
this.bmap = bitMapSet;
//Log.d("Bitmap bmap value", "bmap="+bmap);
}
Bitmap returnBitmap()
{
//Log.d("Bitmap bmap value", "bmap="+bmap);
return bmap;
}
}
In spite of doing the following in my activity, the returnBitMap() returns null.
GetBitMapFromURL gbmap1 = new GetBitMapFromURL(); //Obtain medium bitmap
gbmap1.execute(applicationImageMediumURL);
if(gbmap1.getStatus() == AsyncTask.Status.FINISHED)
{
applicationMediumBitMap = gbmap1.returnBitmap();
}
Suggest me as to where I’m going wrong.
Don’t do that, use the AsyncTask.onPostExecute() method to update the UI like
and remove the code
in the Activity onCreate() (I guess). Put any following code to this on its own Activity method and call it within onPostExecute().