1

I have an Activity, which holds a TabHolder. Every tab its a Fragment.

One of the tabs, has a ListView, which should be fulfilled with photos, and text. The problem is, when I tap in the tab, the UI takes a while to load, because I have to get the photos by the URI, resize the bitmap to an ImageView in the adapter in the ListView, and present it. I would like to put the loading of the photos in a thread, and present a ProgressDialog to the user, while the photographs loads. But I don't know where! If I do it in getView method in the adapter, it starts to behave strangely, because it is called as many times as photos in the array...tried doing this in OnCreateView(), inside the Fragment:

adapter=new FotoAdapter(getActivity(), arrayFotos);
    final ProgressDialog dialog=ProgressDialog.show(getActivity(), getString(R.string.onemoment), getString(R.string.fotoloading));
    handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what==1){
                dialog.dismiss();
            }
        }
    };

    Thread thread=new Thread(new Runnable() {
        @Override
        public void run() {
            listaFotos.setAdapter(adapter);
            Message message=new Message();
            message.what=1;
            handler.sendMessage(message);
        }
    });
    thread.start();

...but it does nothing, no dialog is shown, and takes the same time to present the UI to the user.

How could I achieve it?

Thank you.

EDIT:

After suggestion, tried Picasso. Here you can see both approaches, with Picasso and without it:

String path=fotos.get(position).getFotoPath();
//Picasso.with(c).load(path).resize(80, 80).into(holder.foto);
Bitmap foto= BitmapFactory.decodeFile(path);
int bmWidth=foto.getHeight();
int bmHeight=foto.getHeight();
int ivWidth;
int ivHeigth;
int new_width;
int new_height;
ivWidth=dpToPx(80);
new_width=ivWidth;
new_height = (int) Math.floor((double) bmHeight *( (double) new_width / (double) bmWidth));
Bitmap newbitMap = Bitmap.createScaledBitmap(foto, new_width, new_height, true);
holder.lat.setText(fotos.get(position).getLatitud().toString());
holder.lon.setText(fotos.get(position).getLongitud().toString());
holder.foto.setImageBitmap(newbitMap);

Using Picasso, I cannot see the image in the ImageView.

Fustigador
  • 6,339
  • 12
  • 59
  • 115

2 Answers2

1

A. Don't set any views on a thread that isn't the UI thread (as u did).

B. Use Picasso for image loading or some other lib, don't reinvent the wheel. The lib will do all of the loading on back thread therefore you wouldn't need any progressbar (probably).

Follow those two steps and your code should work fine.

Edit: The issue is your path probably, I've used picasso to load urls not from inner storage. Print your path and take a look.

EE66
  • 4,601
  • 1
  • 17
  • 20
  • Tried Picasso...with this line of code 'Picasso.with(c).load(path).resize(80, 80).into(holder.foto);' I have no delay in the UI...but the pic is not displayed in the ImageView. – Fustigador Jul 29 '15 at 13:07
  • from which thread did u call it? u have other issues maybe. but library is the way. – EE66 Jul 29 '15 at 13:09
  • Then there is another issue because Picasso should work. – EE66 Jul 29 '15 at 13:11
  • I don't think the path is wrong, I printed it, and this is: /storage/emulated/0/imageneslpi/Image-5730.jpg. It works without Picasso, so I think the path is ok... – Fustigador Jul 30 '15 at 05:56
0

In case someone is trying to load images from the device, and is facing the same problem that me, this is the correct syntax to use Picasso with that images:

Picasso.with(c).load(new File(path).resize(80, 80).into(holder.foto);
Fustigador
  • 6,339
  • 12
  • 59
  • 115