ulyk
ulyk

Reputation: 53

How to load an image url to an imageview wherein the image view design declared inside a thread?

@SuppressWarnings({ "rawtypes" })
        public void addAttendance(ArrayList<Properties> attendanceusers) {
            //tl.removeView(tr);
            tl.removeAllViews();
            //addHeaderAttendance();
            ctr=0;
            for (Iterator i = attendanceusers.iterator(); i.hasNext();) {

                Properties p = (Properties) i.next();

                property_list.add(p);
                /** Create a TableRow dynamically **/
                tr = new TableRow(this);

                picurl=p.getPic();
                profile = new ImageView(this);

                profile.setPadding(20,50,20,50);
              /*  URL newurl = new URL(picurl); 
                bmp = BitmapFactory.decodeStream(newurl.openConnection().getInputStream()); 
                profile.setImageBitmap(bmp);*/
                try {

                      Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(picurl).getContent());
                      profile.setImageBitmap(bitmap); 
                    } catch (MalformedURLException e) {
                      e.printStackTrace();
                    } catch (IOException e) {
                      e.printStackTrace();
                    }

                profile.setOnClickListener(this);

                //myButton.setPadding(5, 5, 5, 5);
                Ll = new LinearLayout(this);

                params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT);
                params.setMargins(0, 0, 0, 0);

                Ll.setPadding(0, 0, 20, 0);
                Ll.addView(profile,params);
                tr.addView((View)Ll);


                 // Add the TableRow to the TableLayout
                tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
            }
        }

My code for populating my image is not working because my main method is working on a thread. Can you suggest a code that i can put on the a method ? This post is not a duplicate of any other posts here on stack. I tried this code on my other class which is working, however it will blink and blink if the main method(which im using is a thread):

new AsyncTask<Void, Void, Void>() {                  
                    @Override
                    protected Void doInBackground(Void... params) {
                        try {
                            InputStream in = new URL(picurl).openStream();
                            bmp = BitmapFactory.decodeStream(in);
                        } catch (Exception e) {
                           // log error
                        }
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void result) {
                        if (bmp != null)
                            profile.setImageBitmap(bmp);
                    }

               }.execute();

However if i put it inside the first code that i pasted. The ui will constantly blink because it is a thread that is inside a thread. My image to is too large i cant resize it properly can you help me? i will promise to give a check and a plus for those who can help me, and for those who efforted i will also give a point

Upvotes: 1

Views: 33

Answers (1)

Leonid Veremchuk
Leonid Veremchuk

Reputation: 1963

I think you do not need use threads or somethink like this.

Just add to your project Picasso or Glide

This libraries support images loading from resources, memory or outside links from web. This will solve the problems with blink and it is a good solution for caching and safe content load.

Upvotes: 1

Related Questions