-1

I am trying to display an image in listview from url using JSON parsing. the image url displays correctly in log. when i am trying to download image and display in list getting NullPointerException in bmImage.setImageBitmap(result); i am using following code can anyone tell me the solution..

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

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

    protected Bitmap doInBackground(String... urls) {

        Bitmap bt_img = null;


        try {
             FileInputStream in = new FileInputStream(urls[0]);

             InputStream in = new java.net.URL(urls[0]).openStream();
             bt_img = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
             Log.e("Error", e.getMessage());
             e.printStackTrace();
        }
        return bt_img;
    } 

    protected void onPostExecute(Bitmap result) {
        try {
            bmImage.setImageBitmap(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
LomE999
  • 119
  • 16
Namitha
  • 11
  • 1
  • 2
  • Use Android Query for this dear, its very very efficient and working very fine – Pratik Dasa Aug 19 '14 at 09:23
  • Visit here: here is my answer with my name there: http://stackoverflow.com/questions/541966/how-do-i-do-a-lazy-load-of-images-in-listview/17917897#17917897, check it and do like that way, it will work fine I am damn sure – Pratik Dasa Aug 19 '14 at 09:23
  • try using Universal Image Loader library for asynchronously loading images.All you have to do is pass the URL of the image and the imageview and you will get your desired results – williamj949 Aug 19 '14 at 10:11

3 Answers3

0

If you are getting a nullPointerException on the bmImage variable, it means it has not been initialized. As in the code you provide you are not getting its reference, you have to be passing it to the AsyncTask.

What is the code where you pass the bmImage reference, invoking your AsyncTask? The problem seems to be in that code, not in the AsyncTask itself.

0
 1)  best way is use Lazy Loding 
    and
 2) second way is try this code,
    try {
            URL imageURL = new URL(imgUrl);
            qrBitmap = BitmapFactory.decodeStream(imageURL.openStream());

            image.setImageBitmap(qrBitmap);

            } catch (Exception e) {
            Log.d("QRDisplay", e.getMessage());
            }
-1

You have to use the Universal Image Loader for getting the images from the server.

this link help to u

https://github.com/nostra13/Android-Universal-Image-Loader