0

In my app, when you click on the button then I am downloading the images from the url's. It displaying the images some time correctly but some time it display empty. *problem is some times bitmap object(i.e, "result" in code ) return null.*please can anybody help me.

Following is my code

try
            {

          String ImageUrl = ((eachReview)RB_Constant.revht.get(title_value)).UserImageUrl;  
          System.out.println("Image Url:"+ImageUrl);
          if(ImageUrl != null)
          {
              DownLoadImageInAThreadHandler(ImageUrl,holder);                                                                       
          }
          else
          {
              System.out.println("Image url is null then display the default image");
              holder.userImage.setImageResource(R.drawable.defaultuserimage);
          }
       }
       catch(Exception e){
           System.out.println("Error from Userimage fetching.."+e.toString());
       }



private void DownLoadImageInAThreadHandler(final String imgurl,final ViewHolder holder)
 { 



    //Thread for getting the attributes values
     Thread t = new Thread() 
     {
         public void run()
         {                      
             try
             {

                 Bitmap drawable = getDrawableFromUrl(imgurl);                      
                 System.out.println("Drawable(after downloading):"+drawable);                        
                 if(drawable != null)
                 {                                              
                    holder.userImage.setImageBitmap(drawable);                      
                 }
                 else
                 {
                     System.out.println("after downloading drawable is null then set the default image");
                     holder.userImage.setImageResource(R.drawable.defaultuserimage);
                 }                                               
             }
             catch(Exception exp)
             {
                 System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
             }
         }

         private Bitmap getDrawableFromUrl(String imageUrl)
         {                               
                 try 
                 {
                     URL myFileUrl = new URL(imageUrl);
                     HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();                       
                     conn.setDoInput(true);
                     conn.connect();
                     InputStream is = conn.getInputStream();
                     final Bitmap result = BitmapFactory.decodeStream(is);
                     is.close();
                     new Thread() {
                         public void run() {
                             ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                             if(result != null)
                             {
                                 result.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                             }                                                                                                  
                         }
                     }.start();                         
                     return result;
                 } 
                 catch (FileNotFoundException e)
                 {
                     e.printStackTrace();
                 }
                 catch (MalformedURLException e)
                 {
                     e.printStackTrace();
                 } 
                 catch (IOException e) 
                 {
                     e.printStackTrace();
                 }
                 return null;                               
         }                                                  
     };
     t.start();                             
}
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
naresh
  • 10,332
  • 25
  • 81
  • 124
  • Have you checked the image Url in browser? Is image available there? It may be possible that image you are trying to download is not available on the URL provided. – Usama Sarwar Dec 29 '11 at 10:36
  • yes,the image is available. plz check my thread calling is correct? – naresh Dec 29 '11 at 11:02
  • Have you considered switching to picasso ? [here] http://stackoverflow.com/questions/22330772/why-to-use-android-picasso-library-to-download-the-images – Sarpe Mar 11 '14 at 16:50

1 Answers1

0

Try making you getDrawableFromUrl as:

public static Drawable getDrawableFromUrl(String url) {

    Drawable image = null;

    try {
        InputStream in = (java.io.InputStream) new java.net.URL(url).getContent();
        if (in != null) {
            image = Drawable.createFromStream(in, "image");
            in.close();
        }
    } catch (Exception ex) {
         ex.printStackTrace();

    }
    return image;
}

Also do not compress image within same function. Try Doing it after you successfully receive an image. One thing more that write getDrawableFromUrl function out of the thread but in same class. Hope this works for you.

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • Still some time it returns null – naresh Dec 29 '11 at 12:23
  • This is strange. The reason seems that image isn't available at the url that i told you earlier. but according to you image is available. Ok tell me randomly images become null or same images are null every time? – Usama Sarwar Dec 29 '11 at 13:24
  • One thing...are your images .gif ? because sometimes .gif images create problem. Images are not null but still the do not show up. – Usama Sarwar Jan 02 '12 at 07:04
  • .jpg, .png or .gif ??? because all are types of bitmap. See documentation. http://developer.android.com/guide/topics/resources/drawable-resource.html – Usama Sarwar Jan 02 '12 at 10:04
  • url is like this http://www.roadbrake.com/ImageServer/ThumbImage/00e46e81-7587-4ee3-a0b3-513a71fb2bfc – naresh Jan 02 '12 at 12:15