Android 毕加索图像错误
我正在使用毕加索在我的android应用程序中显示来自服务器的图像。我从服务器获取了5个图像URL(HTTP表单),并将其存储在字符串值中。如果我向毕加索发送了正确的链接(.jpg表单),它将正确运行并在我的imageview中显示我的图像,如果以(.pdf表单)发送了错误的链接,它将在我的图像视图中显示错误,但当我从服务器传递null值或空值来字符串时,我的应用程序会首先崩溃其running if语句,即使其值为null或空else语句未运行,我应该在代码中更新什么,以便如果我从服务器获得null值,我的imageview应该显示,并且错误和文本视图值应该更改 //仅在“我的如果/否则”开始时编码:Android 毕加索图像错误,android,android-studio,picasso,Android,Android Studio,Picasso,我正在使用毕加索在我的android应用程序中显示来自服务器的图像。我从服务器获取了5个图像URL(HTTP表单),并将其存储在字符串值中。如果我向毕加索发送了正确的链接(.jpg表单),它将正确运行并在我的imageview中显示我的图像,如果以(.pdf表单)发送了错误的链接,它将在我的图像视图中显示错误,但当我从服务器传递null值或空值来字符串时,我的应用程序会首先崩溃其running if语句,即使其值为null或空else语句未运行,我应该在代码中更新什么,以便如果我从服务器获得nu
if (image_fourth != null && image_fourth != ""){
Picasso.get().load(image_fourth).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image1);
image1.setVisibility(View.VISIBLE);
buttons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (image_second == null){
image_2t.setText("Image Not Found");
image_2t.setVisibility(View.GONE);
}
else if (image_second != null){
Picasso.get().load(image_second).resize(200, 200).placeholder(R.drawable.placeholder).error(R.drawable.error).into(image2);
image2.setVisibility(View.VISIBLE);
image_2t.setText("Image 2");
image_2t.setVisibility(View.VISIBLE);
}
}
});
}
else{
image_1t.setText("Image Not Found");
}
您可以尝试反转if/else语句。据我所知,毕加索不能在load()中使用null或空字符串。在您的情况下,如果字符串/url源为空,则if语句可能会覆盖,您可以加载占位符:
if (url == null || url.isEmpty()) {
Picasso.with(context).load(placeholder).transform(transformation).into(this)
} else {
Picasso.with(context).load(url).error(placeholder).transform(transformation)
.placeholder(placeholder).into(this)
}
另一种选择可能是保持if语句不变,但将其添加到else语句中:
else
image_fourth.setImageResources(R.mipmap.ic_launcher);
我在Glide中使用了类似的技术。这对我很管用。有关更多信息,您可以查看此stackoverflow答案:只需在语句之间切换,然后检查它是否工作
if (image_fourth == null || image_fourth == ""){
//write your else statement here
}else{
//And if code is here
}
使用Glide而不是Picasso,则无需添加任何其他条件或代码来检查字符串是否为空, 检查一下这个样品
Glide.with(activity_context)
.load(your_url)
.placeholder(R.drawable.default_image)
.error(R.drawable.default_image)
.override(200, 200)
.centerCrop()
.dontAnimate()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(mHolder.imgIcon);
你可以找到更多关于Glide的信息
如果您想继续使用毕加索,请尝试此代码,这可能会对您有所帮助
if(!TextUtils.isEmpty(Url)) {
Picasso.with(activity).load(Url.replace(" ", "%20")).error(R.drawable.default_image).networkPolicy(NetworkPolicy.NO_CACHE)
.memoryPolicy(MemoryPolicy.NO_CACHE)
.into(imageView, new Callback() {
public void onSuccess() {
System.out.println(" 02062015:onSuccess");
}
@Override
public void onError() {
imageView.setImageResource(R.drawable.default_image);
System.out.println(" 02062015:onError");
}
});
}
发生了什么类型的错误?@BharatVankar如果我的字符串为null,它的点错误将出现在行“Picasso.get().load(image_fourth)。resize(200200)。placeholder(R.drawable.placeholder)。error(R.drawable.error)。into(image1);“thnku先生,您能帮我解决我正在处理的这个新问题吗?”??问题链接:不幸的是,我对截击不太了解,所以我不确定我是否能在下一次截击中帮助你们