0

I previously worked on fetching image from sd card displaying it in a list view, that worked using:

imgView.setImageURI(Uri.parse(ImagePath));

Now, I am trying to display image from URL, with the following lines but the image is not displayed in the list view, the following are the lines used:

imgView.setImageBitmap(getBitmapFromURL(ImagePath));

Where, getBitmapFromURL is:

public static Bitmap getBitmapFromURL(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;
        }
    }

There is no exception displayed, the image just not get displayed.

Need of an urgent solution....

Thanks,

AliR
  • 2,065
  • 1
  • 27
  • 37

2 Answers2

3

This is a synchronous loading.(Personally I would not use this cause if there are so many Image to be loaded, the apps is a bit laggy)..

URL url = new URL(//your URL);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
imageView.setImageBitmap(bmp);//your imageview

If I were you I would study Async or the lazy adapter..

EDIT I forgot where I got these code (well thank you for a wonderful code author)

Here it is

  public Bitmap getBitmap(String bitmapUrl) {
      try {
        URL url = new URL(bitmapUrl);
        return BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      }
      catch(Exception ex) {return null;}
    }

    public enum BitmapManager {
    INSTANCE;

    private final Map<String, SoftReference<Bitmap>> cache;
    private final ExecutorService pool;
    private Map<ImageView, String> imageViews = Collections
            .synchronizedMap(new WeakHashMap<ImageView, String>());
    private Bitmap placeholder;

    BitmapManager() {
        cache = new HashMap<String, SoftReference<Bitmap>>();
        pool = Executors.newFixedThreadPool(5);
    }

    public void setPlaceholder(Bitmap bmp) {
        placeholder = bmp;
    }

    public Bitmap getBitmapFromCache(String url) {
        if (cache.containsKey(url)) {
            return cache.get(url).get();
        }

        return null;
    }

    public void queueJob(final String url, final ImageView imageView,
            final int width, final int height) {
        /* Create handler in UI thread. */
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                String tag = imageViews.get(imageView);
                if (tag != null && tag.equals(url)) {
                    if (msg.obj != null) {
                        imageView.setImageBitmap((Bitmap) msg.obj);
                    } else {
                        imageView.setImageBitmap(placeholder);
                        Log.d(null, "fail " + url);
                    }
                }
            }
        };

        pool.submit(new Runnable() {

            public void run() {
                final Bitmap bmp = downloadBitmap(url, width, height);
                Message message = Message.obtain();
                message.obj = bmp;
                Log.d(null, "Item downloaded: " + url);

                handler.sendMessage(message);
            }
        });
    }

    public void loadBitmap(final String url, final ImageView imageView,
            final int width, final int height) {
        imageViews.put(imageView, url);
        Bitmap bitmap = getBitmapFromCache(url);


        // check in UI thread, so no concurrency issues
        if (bitmap != null) {
            Log.i("inh","Item loaded from cache: " + url);
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageBitmap(placeholder);
            queueJob(url, imageView, width, height);
        }
    }

    private Bitmap downloadBitmap(String url, int width, int height) {
        try {
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                    url).getContent());

            bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
            Log.i("nandi2 ako", ""+bitmap);
            cache.put(url, new SoftReference<Bitmap>(bitmap));
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}

Now to call it

String fbAvatarUrl = "//Your URL";

    BitmapManager.INSTANCE.loadBitmap(fbAvatarUrl, //Your ImageView, 60,60);
            //60 60 is my desired height and width 
Androyds
  • 391
  • 1
  • 3
  • 20
  • @AliRajput This is an old code and I far as I can remember there are a lots of more sophisticated codes and libraries pop out after thesse – Androyds Sep 24 '12 at 06:18
  • @AliRajput Log.d(null, "fail " + url); on public void queueJob(final String url, final ImageView imageView, I think it is better if you use debugger to find out whats the error ^_^ – Androyds Sep 24 '12 at 06:42
  • @AliRajput send me the URL link – Androyds Sep 24 '12 at 06:42
  • That seems to be the problem.. I think it is either on SD card or get it on web. If your only using Local domain, I would suggest you use Wampserver etc. to test your URL. – Androyds Sep 24 '12 at 07:05
  • Thanks for the code, the issue was not the code it was the forward slash (I wrote the address incorrectly using back word slash) but thanks the code require the lazy adaptor or Async functionality as the no of images would increase a lot later on.. – AliR Sep 25 '12 at 00:55
1

I encountered this kind problem before, you can refer to this thread, if no luck, try my code,

 public static Bitmap loadImageFromUrl(String url) {
        URL m;
        InputStream i = null;
        BufferedInputStream bis = null;
        ByteArrayOutputStream out =null;
        try {
            m = new URL(url);
            i = (InputStream) m.getContent();
            bis = new BufferedInputStream(i,1024 * 8);
            out = new ByteArrayOutputStream();
            int len=0;
            byte[] buffer = new byte[1024];
            while((len = bis.read(buffer)) != -1){
                out.write(buffer, 0, len);
            }
            out.close();
            bis.close();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        byte[] data = out.toByteArray();    
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);        
        return bitmap;
    }
Community
  • 1
  • 1
jaredzhang
  • 1,158
  • 8
  • 12
  • probably your code would be working, but I am displaying images into a listview so somehow the it does not work; can not show the log now as I have progressed quite far in debugging one other code using Async as the no. of images might go way beyond as expected. Thanks anyways – AliR Sep 24 '12 at 06:54