Kevin
Kevin

Reputation: 51

Android AsyncTask will not work downloading image

I am hoping someone can help me with a problem I have been having. I am in the process of making my app work on versions greater than 3.0 so I can only perform GUI tasks on the UI Thread. I have got the following code, I am getting no compile errors but it is not working. In the log I get the following error:

I/AndroidRuntime(464): NOTE: attach of thread 'Binder Thread #3' failed

Thank you for your help!

new DownloadImageTask().execute(imgURL); //imgURL is declared as string URL

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

     protected void onPostExecute(Bitmap result) {
        ((ImageView) findViewById(R.id.imgCity)).setImageBitmap(bmp);
     }

    protected Bitmap doInBackground(String... params) {
        return loadImage(params[0]);
    }

}


public Bitmap loadImage(String poiURLimg) {

    try {
        URL ulrn = new URL(poiURLimg);
        HttpURLConnection con = (HttpURLConnection) ulrn.openConnection();
        InputStream is = con.getInputStream();
        bmp = BitmapFactory.decodeStream(is);
        if (null != bmp)
        return bmp;
    } catch (Exception e) {
    }
    return bmp;
}

Upvotes: 0

Views: 791

Answers (2)

drewhannay
drewhannay

Reputation: 1020

A couple things to try. First, make sure you've added the INTERNET permission to your AndroidManifest.xml file. You'll need that to download images from the internet and your app will crash without it (although it doesn't sound like this is your issue)

Try using this for your loadImage() method:

public static Bitmap loadImage(String src)
    {
        try
        {
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

Here is an example of a DownloadImageTask that I wrote and use in my apps currently (so I know it works correctly):

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
    @Override
    protected Bitmap doInBackground(String... params)
    {
        return loadImage(params[0]);
    }

    @Override
    protected void onPostExecute(Bitmap result)
    {
        if (isCancelled() || result == null)
            return;

        ((ImageView) findViewById(R.id.image_view)).setImageBitmap(result);
    }
}

Upvotes: 0

Rarw
Rarw

Reputation: 7663

The Binder Thread #3 error is not related to the code in your app. There are a number of potential causes usually related to something in eclipse. You can read this post which gives some examples.

As to why the Bitmap wont load - in your onPostExecute you are setting the Bitmap for that ImageView to bmp. Bmp is name of the value that your loadImage method creating the bitmap returns. It is not the name of the Bitmap you pass as an argument into onPosExecute - that is Bitmap result. Change bmp to result and it should work.

Upvotes: 1

Related Questions