0

I'm trying to use Picasso to download an image then place it in the background of my FrameLayout. Why doesn't this work?

FrameLayout:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/coverBackground"
        android:layout_width="match_parent"
        android:layout_height="96dp"
        android:layout_gravity="top|center_horizontal">

        <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:background="@drawable/transparent_gradient"
            android:minHeight="30dp"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp" />

    </FrameLayout>

Method to download cover image:

private void downloadCoverBackground(FrameLayout coverBackground) {

 ParseUser currentUser = ParseUser.getCurrentUser();

 if (currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND) != null) {

  String coverBackgroundURL = currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND).getUrl();

  // Asynchronously display the cover background image downloaded from Parse
  if (coverBackgroundURL != null) {

   Picasso.with(getApplicationContext())
    .load(coverBackgroundURL)
    //.networkPolicy(NetworkPolicy.OFFLINE)
    .placeholder(R.color.placeholderblue)
    .fit()
    .into(new Target() {

     @Override
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
       coverBackground.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
      }
     }

     @Override
     public void onBitmapFailed(final Drawable errorDrawable) {
      Log.d("TAG", "FAILED");
     }

     @Override
     public void onPrepareLoad(final Drawable placeHolderDrawable) {
      Log.d("TAG", "Prepare Load");
     }
    });

  } else {
   coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
  }
 } else {
  coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
 }

}

Error:

Caused by: java.lang.IllegalStateException: Fit cannot be used with a Target.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java: 502)
at com.yitter.android.activity.UserProfileActivity.downloadCoverBackground(UserProfileActivity.java: 160)
at com.yitter.android.activity.UserProfileActivity.onCreate(UserProfileActivity.java: 90)
at android.app.Activity.performCreate(Activity.java: 6288)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java: 1113)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2500)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java: 2613) 
at android.app.ActivityThread.access$900(ActivityThread.java: 180) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java: 1473) 
at android.os.Handler.dispatchMessage(Handler.java: 111) 
at android.os.Looper.loop(Looper.java: 207) 
at android.app.ActivityThread.main(ActivityThread.java: 5710) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 900) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 761) 

What's going wrong here?

Martin Erlic
  • 5,467
  • 22
  • 81
  • 153

1 Answers1

1

remove .fit() I attach code after remove that

private void downloadCoverBackground(FrameLayout coverBackground) {

 ParseUser currentUser = ParseUser.getCurrentUser();

 if (currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND) != null) {

  String coverBackgroundURL = currentUser.getParseFile(ParseConstants.KEY_COVER_BACKGROUND).getUrl();

  // Asynchronously display the cover background image downloaded from Parse
  if (coverBackgroundURL != null) {

   Picasso.with(getApplicationContext())
    .load(coverBackgroundURL)
    //.networkPolicy(NetworkPolicy.OFFLINE)
    .placeholder(R.color.placeholderblue)
    .into(new Target() {

     @Override
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
       coverBackground.setBackground(new BitmapDrawable(getApplicationContext().getResources(), bitmap));
      }
     }

     @Override
     public void onBitmapFailed(final Drawable errorDrawable) {
      Log.d("TAG", "FAILED");
     }

     @Override
     public void onPrepareLoad(final Drawable placeHolderDrawable) {
      Log.d("TAG", "Prepare Load");
     }
    });

  } else {
   coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
  }
 } else {
  coverBackground.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color.stroke));
 }

}
Upendra Shah
  • 2,218
  • 17
  • 27