Skip to content Skip to sidebar Skip to footer

Downloaded Image Can Not Be Displayed

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 fr

Solution 1:

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)..

URLurl=newURL(//your URL);Bitmapbmp= 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 {
        URLurl=newURL(bitmapUrl);
        return BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
      }
      catch(Exception ex) {returnnull;}
    }

    publicenumBitmapManager {
    INSTANCE;

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

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

    publicvoidsetPlaceholder(Bitmap bmp) {
        placeholder = bmp;
    }

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

        returnnull;
    }

    publicvoidqueueJob(final String url, final ImageView imageView,
            finalint width, finalint height) {
        /* Create handler in UI thread. */finalHandlerhandler=newHandler() {
            @OverridepublicvoidhandleMessage(Message msg) {
                Stringtag= 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(newRunnable() {

            publicvoidrun() {
                finalBitmapbmp= downloadBitmap(url, width, height);
                Messagemessage= Message.obtain();
                message.obj = bmp;
                Log.d(null, "Item downloaded: " + url);

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

    publicvoidloadBitmap(final String url, final ImageView imageView,
            finalint width, finalint height) {
        imageViews.put(imageView, url);
        Bitmapbitmap= getBitmapFromCache(url);


        // check in UI thread, so no concurrency issuesif (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 {
            Bitmapbitmap= BitmapFactory.decodeStream((InputStream) newURL(
                    url).getContent());

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

        returnnull;
    }

}

Now to call it

StringfbAvatarUrl="//Your URL";

    BitmapManager.INSTANCE.loadBitmap(fbAvatarUrl, //Your ImageView, 60,60);//60 60 is my desired height and width 

Solution 2:

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

publicstatic 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 = newbyte[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;
    }

Post a Comment for "Downloaded Image Can Not Be Displayed"