【问题标题】:Picasso not showing images毕加索不显示图像
【发布时间】:2016-12-11 00:31:16
【问题描述】:

我有一个从内部存储中获取图像的应用,并且必须在 ImageView 中显示它们。由于图像的加载会对应用程序的性能产生影响,因此我想异步进行。我提出了this 的问题,并被告知使用毕加索。该应用程序现在运行得很快,但没有显示图像。这是我用来将图像放入 ImageView 的代码:

Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);

这是适配器,带有我在没有毕加索的情况下使用的注释行。这样,图像就会显示出来,但会延迟 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;
    }
}

使用 Picasso 并显示图像会很棒,但如果不可能,我该怎么办?

谢谢。

【问题讨论】:

    标签: android imageview picasso


    【解决方案1】:

    将此用于:

    从内部存储中获取

    Picasso.with(c).load(new File(path)).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
    

    从网址加载

    Picasso.with(c).load(path).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
    

    【讨论】:

    • 谢谢老兄,那是救命稻草。
    【解决方案2】:

    你应该解析 uri 的路径,然后将图像加载为

    pathUri = Uri.fromFile(new File(path));
    Picasso.with(c).load(pathUri).resize(dpToPx(80), dpToPx(80)).centerCrop().into(holder.foto);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 2019-12-12
      • 1970-01-01
      • 2014-08-29
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多