Search code examples
androidpicasso

Picasso Not Loading image from url


So I am trying to simply load an image from a url(its valid) to an imageView using Picasso. I have added the Picasso libary via gradle and I have added both:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" /> 

to my code as many other users have pointed out. I don't have any errors and it simply doesn't do anything even after 10+ seconds. Here is my Java method:

ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image=(ImageView)findViewById(R.id.image);
    Picasso.with(getApplicationContext()).load("http://i.imgur.com/DvpvklR.png").into(image);
}

and my activity XML:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.benhouse.testproj.MainActivity">


<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/image"/>
</RelativeLayout>

Any ideas on how to get it to work? I'm sure I'm making an obvious mistake somewhere.


Solution

  • Your parent Relative Layout height and width is 0dp that's why childLayout(ImageView) unable to show image .So, you need to set Relative layout height and width wrap_content or match_parent. like that:

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.benhouse.testproj.MainActivity">
    
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:id="@+id/image"/>
    </RelativeLayout>