0

I'm trying to load an image (JPG) from a url using the following asynctask method.

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

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }

I'm executing this method in onCreate like this

new DownloadImageTask((ImageView) findViewById(R.id.imageView1)).execute(final_url);

But I can't see any image and when I debugged it, I found the value of mIcon11 to be null. What am I doing wrong

PS : The final_url goes like this

www.website.com/somelink/IMG 005_153.JPG
  • btw you have to encode your url because it has a space ;), Where are you executing this? in onCreate? Try to init your ImageView in oncreate and store it as global value in your activity and pass it to your task – A.S. Apr 09 '14 at 11:13
  • Guess that's the problem. Can you tell me a way to do that? – user2028405 Apr 09 '14 at 11:22

2 Answers2

0

Your finalurlhttp://www.website.com/somelink/IMG%20005_153.JPG is not returning any thing. Check that..

Try to encode it before using it.

String uri = Uri.parse("http://...")
            .buildUpon()
            .appendQueryParameter("key", "val")
            .build().toString();

or try following

String uri = URLEncoder.encode(finalurl, "utf-8");

Systematix Infotech
  • 2,345
  • 1
  • 14
  • 31
0
private class ImageLoadTask extends AsyncTask<String, Void, Bitmap> {
        private Bitmap bitmap;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            pb1.setVisibility(View.VISIBLE);
            super.onPreExecute();
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            // TODO Auto-generated method stub
            try {
                URL url;
                url = new URL(params[0]);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                bitmap = BitmapFactory.decodeStream(input);
            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            im.setImageBitmap(result);
            pb1.setVisibility(View.INVISIBLE);
        }
    }

and call it like

new ImageLoadTask().execute(STRING URL);
Android
  • 8,995
  • 9
  • 67
  • 108