0

How can I load an image using picasso library? I already tried it but it display force close on my screen. I just added this:

ImageView a = (ImageView)findViewById(R.id.ivImage);
Picasso.with(Home.this).load(TAG_IMAGE).into(a);  

On my onPostExecute(), I am loading my image from server.

Renz Manacmol
  • 869
  • 12
  • 27

3 Answers3

0

For that purpose, you have to write custom adapter.

Refer this link to know how to use Picasso in your custom adapter.

To learn about customAdapter() refer this link

Community
  • 1
  • 1
Paritosh
  • 2,097
  • 3
  • 30
  • 42
  • Use your Picasso.with()... statement there. – Paritosh Aug 17 '14 at 10:03
  • Refer the link in updated answer. Check CustomListAdapter.java on the page. – Paritosh Aug 17 '14 at 11:43
  • Please ask, if you are not getting the logic of BaseAdapter. – Paritosh Aug 17 '14 at 11:52
  • Yes, you have to create object of CustomListAdapter like CustomListAdapter adaptorObj = new CustomListAdapter (getActivity(), dataList); -- getActivity() , dataList are parameters. – Paritosh Aug 17 '14 at 13:53
  • Are you storing image in database ? In this case it is not possible to get the image using Picasso. You have to store image as file on your server. Store path of your image in database. – Paritosh Aug 17 '14 at 14:43
  • pass full path of image to load() Eg. Picasso.with(context).load("http://example.com/imgFolder/imageName.png").into(imageView); it will work – Paritosh Aug 18 '14 at 04:20
  • You can divide the string as basePath - path of your website Eg. www.example.com Again you can use TAG_IMAGE - image path stored in your database. In my Eg. bellow I am doing same thing. I am keeping basePath and just passing imagePath to HashMap – Paritosh Aug 19 '14 at 14:26
0

Create new class and name it whatever you want (here AlbumList_Adapter.java)

public class AlbumList_Adapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
String basePath = "http://example.com/imgFolder/";

public AlbumList_Adapter(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader = new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

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

public View getView(int position, View convertView, ViewGroup parent) {
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.list_row_simple, null);

    TextView txtListItem = (TextView) vi.findViewById(R.id.txtListItem);
    ImageView imageView= (ImageView) vi.findViewById(R.id.imageView);

    HashMap<String, String> imgData = new HashMap<String, String>();
    imgData = data.get(position);

    txtListItem.setText(imgData.get("name"));

    Picasso.with(context)
         .load(basePath+ imgData.get("image"))
         .resize(100, 100)
         .centerCrop()
         .into(imageView);

    return vi;
    }

}

In your code.java file, declare adapter at the top (before onCreate() so that you can use it anywhere)

AlbumList_Adapter adapter;

In your code after json parsing

ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();

            // looping through all nodes
            for (int i = 0; i < jsonArray.length(); i++) {

                jsonObject = jsonArray.getJSONObject(i);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap
                map.put("name", jsonObject.getString("albumName"));
                map.put("image", jsonObject.getString("albumImage"));

                // adding HashMap to ArrayList
                dataList.add(map);
            }
adapter = new AlbumList_Adapter(codeActivity.this, dataList);
list.setAdapter(adapter);

Note that, I am using Hashmap. You should use your data structure. (You can use Hashmap. Then you have to update your code)

One more thing, following line in AlbumList_Adapter.java refers to xml file I created for list row.

vi = inflater.inflate(R.layout.list_row_simple, null);

Hope this answer helps you. Please let me know if you got some problem implementing this.

Happy coding...

Paritosh
  • 2,097
  • 3
  • 30
  • 42
0

First you have to get Picasso Instance

val picasso = Picasso.get()

and then you can load with a single line of code.

picasso.load("your url").into(imageView)

For reference follow this link - https://www.androidbytes.in/image-loading-in-android-with-picasso/

honey_ramgarhia
  • 537
  • 7
  • 15