Reputation: 6459
I have an app that gets images from internal storage, and must show them in an ImageView. Since the loading of the images has an impact in performance in the app, I would like to do it asynchronously. I made this question, and was told to use Picasso. The app now goes fast, but is not showing the image. This is the code I am using to put the image in the ImageView:
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
This is the adapter, with the commented lines I used without Picasso. That way, the Image is shown, but it delays the UI:
public class FotoAdapter extends BaseAdapter {
private ArrayList<BeanFotos> fotos;
private LayoutInflater inflater=null;
Context c;
Handler handler;
public FotoAdapter(Context c, ArrayList<BeanFotos> fotos){
this.fotos=fotos;
inflater=LayoutInflater.from(c);
this.c=c;
}
@Override
public int getCount() {
if(fotos!=null){
return fotos.size();
}
return 0;
}
@Override
public Object getItem(int position) {
return fotos.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView=inflater.inflate(R.layout.foto_layout, null);
holder=new ViewHolder();
holder.lat=(TextView)convertView.findViewById(R.id.textlat);
holder.lon=(TextView)convertView.findViewById(R.id.textlon);
holder.foto=(ImageView)convertView.findViewById(R.id.foto);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
String path=fotos.get(position).getFotoPath();
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().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);
return convertView;
}
private int dpToPx(int dp)
{
float density = c.getResources().getDisplayMetrics().density;
return Math.round((float)dp * density);
}
static class ViewHolder {
public TextView lat;
public TextView lon;
public ImageView foto;
}
}
Would be great to use Picasso and show the Image, but if that is not possible, what could I do?
Thank you.
Upvotes: 1
Views: 2684
Reputation: 961
Use this for:
fetching from internal storage
Picasso.with(c).load(new File(path)).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
loading from url
Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
Upvotes: 3
Reputation: 1874
You should parse path to uri then load the image as
pathUri = Uri.fromFile(new File(path));
Picasso.with(c).load(pathUri).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
Upvotes: 1