该应用程序在使用毕加索并发送空白图片时崩溃

The app crashes on using picasso and sending an empty picture

我正在为我的导航抽屉个人资料图片使用 picasso。

我一点击导航抽屉中的显示图片,应用程序就崩溃了。 但是,如果事先在此处上传图片并单击它会显示来自后端的完整用户配置文件,则效果很好。

我的代码是:

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);
    }
}

}

弹出的例外是:

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) 

这基本上是一个 IllegalArgumentException:路径不能留空。

我在一些地方读到 Picasso 无法处理空地址。谁能告诉我如何解决这个问题。

试试这个

 if (!TextUtils.isEmpty(data.getImageUrl())) {
       Picasso.get()
            .load(!TextUtils.isEmpty(data.getImageUrl()) ? APIMethods.LIVE_SERVER + data.getImageUrl() : "")
            .placeholder(R.drawable.ic_user)
            .into(civProfileImage);
    } else {
    civProfileImage.setimageResource(R.mipmap.ic_launcer);
    }

旧版本的 Picasso:

试试这个

Picasso.with(getApplicationContext())
                .load(!TextUtils.isEmpty(data.getImageUrl()) ? APIMethods.LIVE_SERVER + data.getImageUrl() : R.drawable.ic_user)
                .placeholder(R.drawable.ic_user)
                .into(civProfileImage);

或者这个

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);
}

对于新版 Picasso:

试试这个

Picasso.get()
        .load(!TextUtils.isEmpty(data.getImageUrl()) ? APIMethods.LIVE_SERVER + data.getImageUrl() : R.drawable.ic_user)
         .placeholder(R.drawable.ic_user)
         .into(civProfileImage);

或者这个

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);
}

这样试试

if(!TextUtils.isEmpty(data.getImageUrl())) {
                        Picasso.with(data.getImageUrl())
                                .load(R.drawable.ic_user)
                                .into(civProfileImage);
                    }

Picasso 不允许设置空路径。来自你的 Logcat:

 Caused by: java.lang.IllegalArgumentException: Path must not be empty

查看答案here

添加其他答案,Picasso 允许您使用

管理下载错误
Picasso.error(DEFAULT IMAGE);