使用Picasso加载本地图像

时间:2016-01-07 06:51:10

标签: android android-fragments android-gridview picasso

我正在学习使用Picasso在GridView中加载图像。我试图加载局部可绘制图像,但gridview显示为空白。我在其他帖子中检查了解决方案,但没有找到任何令人满意的答案。只找到了this解决方案,该解决方案无效。任何加载本地图像的建议都将非常受欢迎。



Adapter to load images

public class ImageAdapter extends BaseAdapter {
    private Context mContext;


    // Keep all Images in array
    public Integer[] mThumbIds = {};


    public ImageAdapter(Context c){
        mContext = c;
    }


	@Override
    public int getCount() {
        return mThumbIds.length;
    }

    @Override
    public Object getItem(int position) {
        return mThumbIds[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {         
        ImageView imageView = new ImageView(mContext);
        imageView.setImageResource(mThumbIds[position]);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
        
        Picasso.with(mContext).load(mThumbIds[position]).into(imageView);
        return imageView;
    }
}

Fragment having gridview

public class GridFrag extends Fragment{

      public Integer[] mThumbIds = { R.drawable.brazil, R.drawable.colosseum,
            R.drawable.eiffeltower, R.drawable.greatwall, R.drawable.pyramid,
            R.drawable.statue, R.drawable.tajmahal };
	
	@Override
	   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	      
	       //Inflate the layout for this fragment
		
		View v = LayoutInflater.from(getActivity()).inflate(R.layout.frag_grid, null);
		
		GridView gridview = (GridView) v.findViewById(R.id.gridview);
	      gridview.setAdapter(new ImageAdapter(this.getActivity()));
	       
	      return v;
	   }

}




2 个答案:

答案 0 :(得分:1)

您应该将阵列移至ImageAdapter。适配器中的mThumbIds阵列为空

使用以下代码更改您的代码:

public class ImageAdapter extends BaseAdapter {
private Context mContext;


// Keep all Images in array
public Integer[] mThumbIds = { R.drawable.brazil, R.drawable.colosseum,
        R.drawable.eiffeltower, R.drawable.greatwall, R.drawable.pyramid,
        R.drawable.statue, R.drawable.tajmahal };


public ImageAdapter(Context c){
    mContext = c;
}


@Override
public int getCount() {
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    return mThumbIds[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {         
    ImageView imageView = new ImageView(mContext);
    imageView.setImageResource(mThumbIds[position]);
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setLayoutParams(new GridView.LayoutParams(100, 100));

    Picasso.with(mContext).load(mThumbIds[position]).into(imageView);
    return imageView;
}

}

答案 1 :(得分:0)

简单

Uri uri=Uri.fromFile(new File("YOUR IMAGE FILE PATH"));
Picasso.with(mContext).load(uri).placeholder(R.drawable.ic_launcher).error(R.drawable.landscape_1).into("YOUR IMAGE VIEW");

在picasso加载实际图像时显示样本图像的占位符。

错误:如果图像加载失败,则应显示此图像