Somnath Pal
Somnath Pal

Reputation: 190

Load local images using Picasso

I'm learning loading images in GridView using Picasso. I tried to load local drawable images but gridview is showing blank. I checked for solution in other post but didn't find any satisfying answer. Only found this solution which isn't working. Any suggestion to load local images would be much appreciated.

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;
	   }

}

Upvotes: 0

Views: 2151

Answers (2)

Jayanth
Jayanth

Reputation: 6297

Simple

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");

placeholder for showing sample image while actual image is loading by picasso.

error : if the image loading fails this image should be shown

Upvotes: 0

Uniruddh
Uniruddh

Reputation: 4436

You should move your array to ImageAdapter. Your mThumbIds array is empty in Adapter.

Change you code with following:

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;
}

}

Upvotes: 1

Related Questions