I am using picasso for my navigation drawer profile picture.
The app crashes as soon as I click on the display picture in the navigation drawer. However, it is works fine when a picture is uploaded here beforehand and on clicking it shows the complete User Profile, which comes from the back-end.
My code is :
public class ProfileActivity extends BaseActivity {
@InjectView(R.id.svParent)
ScrollView svParent;
@InjectView(R.id.toolbar)
Toolbar mToolbar;
@InjectView(R.id.action_title)
TextView tvActionTitle;
@InjectView(R.id.civProfileImage)
CircleImageView civProfileImage;
@InjectView(R.id.tvName)
TextView tvName;
@InjectView(R.id.tvCity)
TextView tvCity;
@InjectView(R.id.etEmail)
EditText etEmail;
@InjectView(R.id.etPhoneNumber)
EditText etPhoneNumber;
@InjectView(R.id.etAlternatePhoneNumber)
EditText etAlternatePhoneNumber;
@InjectView(R.id.etAddress)
EditText etAddress;
@InjectView(R.id.etDescription)
EditText etDescription;
@InjectView(R.id.llProgressContainer)
LinearLayout llProgressContainer;
public static Intent callingIntent(Context context) {
return new Intent(context, ProfileActivity.class);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
ButterKnife.inject(this);
setParentView(svParent);
setActionBar();
initUserProfile();
}
private void setActionBar() {
setSupportActionBar(mToolbar);
ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setTitle("");
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
tvActionTitle.setText(getResources().getString(R.string.label_user_profile));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void initUserProfile() {
String prefsData = SharedPrefsManager.getUserData(getApplicationContext());
UserData userData = new Gson().fromJson(prefsData, UserData.class);
Data data = userData.getData();
if (data != null) {
String name = data.getName();
String city = data.getCity();
String mobile = data.getMobile();
String alternateMobile = data.getAlternateMobile();
String mail = data.getEmail();
String address = data.getAddress();
if (!TextUtils.isEmpty(name)) {
tvName.setText(name);
} else {
tvName.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(city)) {
tvCity.setText(city);
} else {
tvCity.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(mobile)) {
etPhoneNumber.setText(mobile);
etPhoneNumber.setEnabled(false);
} else {
etPhoneNumber.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(alternateMobile)) {
etAlternatePhoneNumber.setText(alternateMobile);
etAlternatePhoneNumber.setEnabled(false);
} else {
etAlternatePhoneNumber.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(mail)) {
etEmail.setText(mail);
etEmail.setEnabled(false);
} else {
etEmail.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(address)) {
etAddress.setText(address);
etAddress.setEnabled(false);
} else {
etAddress.setVisibility(View.GONE);
}
Picasso.with(getApplicationContext())
.load(!TextUtils.isEmpty(data.getImageUrl()) ? APIMethods.LIVE_SERVER + data.getImageUrl() : "")
.placeholder(R.drawable.ic_user)
.into(civProfileImage);
}
}
}
The exceptions which pop up are:
07-18 08:40:31.268 8668-8668/in.mahabir E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.mahabir, PID: 8668
java.lang.RuntimeException: Unable to start activity ComponentInfo{in.mahabir/in.mahabir.ui.profile.ProfileActivity}: java.lang.IllegalArgumentException: Path must not be empty.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalArgumentException: Path must not be empty.
at com.squareup.picasso.Picasso.load(Picasso.java:194)
at in.mahabir.ui.profile.ProfileActivity.initUserProfile(ProfileActivity.java:154)
at in.mahabir.ui.profile.ProfileActivity.onCreate(ProfileActivity.java:78)
at android.app.Activity.performCreate(Activity.java:6664)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
which is basically an IllegalArgumentException : Path cannot be left blank.
I read in a few places that Picasso cant handle null addresses. Can someone please tell me how to fix this.
For Older Versions of Picasso:
Try this
Picasso.with(getApplicationContext())
.load(!TextUtils.isEmpty(data.getImageUrl()) ? APIMethods.LIVE_SERVER + data.getImageUrl() : R.drawable.ic_user)
.placeholder(R.drawable.ic_user)
.into(civProfileImage);
Or this
try{
Picasso.with(getApplicationContext())
.load(APIMethods.LIVE_SERVER + data.getImageUrl())
.placeholder(R.drawable.ic_user)
.into(civProfileImage);
}catch (Exception e){
e.printStackTrace();
Picasso.with(getApplicationContext())
.load(R.drawable.ic_user)
.into(civProfileImage);
}
For New Version of Picasso:
Try this
Picasso.get()
.load(!TextUtils.isEmpty(data.getImageUrl()) ? APIMethods.LIVE_SERVER + data.getImageUrl() : R.drawable.ic_user)
.placeholder(R.drawable.ic_user)
.into(civProfileImage);
Or this
try{
Picasso.get()
.load(APIMethods.LIVE_SERVER + data.getImageUrl())
.placeholder(R.drawable.ic_user)
.into(civProfileImage);
}catch (Exception e){
e.printStackTrace();
Picasso.get()
.load(R.drawable.ic_user)
.into(civProfileImage);
}