Search code examples
androidandroid-studiopicasso

Image is not displaying when using Picasso in Android


I am using Picasso for a few images and they are working fine, now I am attempting to get an image which I have displayed before but in a new activity. This one is not displaying, I only get the placeholder.

I have tried to remove the fit() and also changed it to resize(325, 275). I have checked if it is getting the url from Parse correctly and it is.

In my onCreate

super.onCreate(savedInstanceState);
    setContentView(R.layout.change_photo);

    mSelectPhoto = (Button) findViewById(R.id.select);
    mSendPhoto = (Button) findViewById(R.id.send);
    mRetake = (Button) findViewById(R.id.retake);
    mDescription = (TextView) findViewById(R.id.description);
    preview = (ImageView) findViewById(R.id.photoPreview);
    div = (ImageView) findViewById(R.id.div);

    mFileType = ParseConstants.TYPE_IMAGE;
    currentUserID = currentUser.getObjectId();

    Log.d("============", "Retrieved " + currentUserID + " scores");

    ParseQuery<ParseObject> query = ParseQuery
            .getQuery(ParseConstants.KEY_USER_INFO);
    query.whereEqualTo(ParseConstants.KEY_USER_ID_INFO, currentUser.getObjectId());
    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for (ParseObject thisUser : objects) {
                    currentPicture = thisUser.getParseFile("profilePicture")
                            .getUrl();
                }
            } else {
                Log.d("EditProfileFragment.jav", "Error: " + e.getMessage());
            }
        }
    });

    Picasso.with(this).load(currentPicture).fit().into(preview);

My XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:text="Select a Photo To Get Started" />

    <ImageView
        android:id="@+id/photoPreview"
        android:layout_width="325dp"
        android:layout_height="275dp"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dp"
        android:layout_below="@+id/description"
        android:contentDescription="profile picture"/>

    <Button
        android:id="@+id/select"
        style="@style/AuthButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/photoPreview"
        android:text="Select a photo"
        android:textColor="#FFFFFF" />

    <Button
        android:id="@+id/send"
        style="@style/AuthButton"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/photoPreview"
        android:layout_toLeftOf="@+id/div"
        android:text="Change Picture"
        android:textColor="#FFFFFF" />

    <ImageView
        android:id="@+id/div"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_margin="1dp"
        android:visibility="invisible"
        android:layout_below="@+id/photoPreview"
        android:src="@drawable/button_divider"/>

    <Button
        android:id="@+id/retake"
        style="@style/AuthButton"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:visibility="invisible"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/photoPreview"
        android:layout_toRightOf="@+id/div"
        android:text="Retake"
        android:textColor="#FFFFFF" />
</RelativeLayout>

Solution

  • You determine the url of your picture in an background task. Your Picasso-Load operation will simply be executed, before the url was loaded in your background task. Try to move your Picasso-Load operation inside your callback:

    query.findInBackground(new FindCallback<ParseObject>() {
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {
                for (ParseObject thisUser : objects) {
                    currentPicture = thisUser.getParseFile("profilePicture")
                            .getUrl();
                    Picasso.with(Your_Activity.this).load(currentPicture).fit().into(preview);
                }
            } else {
                Log.d("EditProfileFragment.jav", "Error: " + e.getMessage());
            }
        }
    });