Search code examples
androidnetworkimageview

Set a bitmap to NetworkImageView


I am creating Imageviews dynamically and displaying it. But i faced reloading problem when scroll so i decided to use NetworkImageView but here i am not able to set the bitmap. It is not showing any error but it is not displaying any images. Below my code...

final NetworkImageView imageView = new NetworkImageView(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(150,
            200);
    params.weight = 1;
    //imageView.setImageBitmap(null);
    imageView.setLayoutParams(params);
    imageView.setMaxHeight(200);
    imageView.setMaxWidth(150);
    // bitmap =//
    // BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_menu_folder);
    // Log.i("Bitmaps Counts", String.valueOf(pos));
    /*    comment all this...
    new Thread() {
        public void run() {


                bitmap = downloadImage(tempValues.getItemAbsolutePath());


            ((Activity) context).runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    imageView.setImageBitmap(bitmap);

                }
            });

        }
    }.start();*/
    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    ImageLoader mImageLoader = new ImageLoader(requestQueue,new LruBitmapCache(1000000));
     mImageLoader.get(tempValues.getItemAbsolutePath(),
            ImageLoader.getImageListener(imageView,
                   R.android.defaultimg,
                   R.android.errorimg));

    layout.addView(imageView);

downloadImage code..

/* unused method
      private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.decodeStream(stream, null, bmOptions);
            stream.close();

        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream getHttpConnection(String urlString) throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }*/

errorlog

05-12 19:11:13.971: E/AndroidRuntime(11177): FATAL EXCEPTION: Thread-7325 05-12 19:11:13.971: E/AndroidRuntime(11177): Process: com.dar.app, PID: 11177 05-12 19:11:13.971: E/AndroidRuntime(11177): java.lang.NegativeArraySizeException: -350 05-12 19:11:13.971: E/AndroidRuntime(11177): at com.android.volley.toolbox.DiskBasedCache.streamToBytes(DiskBasedCache.java:316) 05-12 19:11:13.971: E/AndroidRuntime(11177): at com.android.volley.toolbox.DiskBasedCache.get(DiskBasedCache.java:117) 05-12 19:11:13.971: E/AndroidRuntime(11177): at com.android.volley.CacheDispatcher.run(CacheDispatcher.java:100)


Solution

  • you can use volley image downloder

    it easy default img is set...loader request for image .. if image found using url it will set ..or if error it will set error image..this way you'll inform about the error on loading img

     RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
     ImageLoader mImageLoader = new ImageLoader(requestQueue,new LruBitmapCache(1000000));
     mImageLoader.get(<img-url>,
                    ImageLoader.getImageListener(<imageview-object>,
                           R.drawable.defaultimg,
                           R.drawable.errorimg));