Java 使用毕加索-Android加载多幅图像
我正在尝试使用毕加索库从一些URL加载多个图像 到目前为止,我已经尝试了以下代码:Java 使用毕加索-Android加载多幅图像,java,android,picasso,Java,Android,Picasso,我正在尝试使用毕加索库从一些URL加载多个图像 到目前为止,我已经尝试了以下代码: for(int i = 0; i < friends.size(); i++) { final Profile profile = friends.get(i); String url = profile.getUserImageUrl(); Picasso.with(getContext()).load(url).into(new Target() {
for(int i = 0; i < friends.size(); i++)
{
final Profile profile = friends.get(i);
String url = profile.getUserImageUrl();
Picasso.with(getContext()).load(url).into(new Target() {
// It doesn't reach any of the code below ....!!
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
profile.setUserImage(bitmap);
counter++;
if(counter >= friends.size() - 1)
cards();
}
@Override
public void onBitmapFailed(Drawable drawable) {
Log.e("App", "Failed to load company logo in onBitmapFailed method");
}
@Override
public void onPrepareLoad(Drawable drawable) {
Log.e("App","Failed to load company logo in onBitmapFailed method");
}
});
}
for(int i=0;i=friends.size()-1)
卡片();
}
@凌驾
公共void onBitmapFailed(可提取可提取){
Log.e(“应用程序”,“在onBitmapFailed方法中加载公司徽标失败”);
}
@凌驾
准备就绪时的公共作废(可提取){
Log.e(“应用程序”,“在onBitmapFailed方法中加载公司徽标失败”);
}
});
}
这个代码不起作用。
当我运行此代码时,它不会到达目标接口中的任何行。有人知道为什么吗?也许我这里遗漏了什么,但我认为
into()
只接受ImageView
和可选的回调。你能这样做吗:
Picasso
.with(getContext())
.load(profile.getUserImageUrl())
.into(imageView, new Callback()
{
@Override
public void onSuccess()
{
// Update card
}
@Override
public void onError()
{
Log.e("App","Failed to load company logo");
}
});
但是,让我们试试这个:我猜您可能正在尝试将概要文件图像添加到一组现有视图中,或者在循环所有概要文件时动态尝试创建这些视图。以下是两种情况的解决方案:
for (int i = 0; i < friends.size(); i++)
{
Profile profile = friends.get(i);
if (profile != null)
{
/** Either find an existing view like this: **/
// You're assembling a resource ID here.
String resourceName = "profile_" + profile.getId(); // Assuming you have an ID.
int resourceId = getResources().getIdentifier(resourceName, "id", getActivity().getPackageName());
// Use it to get the image view in your activity's layout.
ImageView imageView = (ImageView) findViewById(resourceId);
if (imageView != null)
{
Picasso
.with(this)
.load(profile.getUserImageUrl())
.into(imageView);
}
/** Or inflate a View like this: **/
// Get a containing view. You should move this above your
// loop--it's here so I can keep these blocks together.
FrameLayout frameLayout = (FrameLayout) findViewById(R.layout.frame_layout);
// This is a layout that contains your image view and
// any other views you want associated with a profile.
View view = LayoutInflater.from(this).inflate(R.layout.profile_layout, null, false);
// You're finding the view based from the inflated layout, not the activity layout
ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
if (imageView != null)
{
Picasso
.with(this)
.load(profile.getUserImageUrl())
.into(imageView);
}
frameLayout.addView(view);
}
}
for(int i=0;i
在请求运行时,只需保留对目标的强引用即可。对于要加载的每张图片,您还需要一个不同的Target
实例(因为,如果我没有弄错的话,如果为同一Target
启动了一个新的Target
,毕加索将取消先前对Target
的请求)
说明:
出现此问题的实际原因是:
注意:此方法保留对目标实例的弱引用,并且
如果您不保持对它的强引用,它将被垃圾收集。
要在加载映像时接收回调,请使用
进入(android.widget.ImageView,回调)
资料来源:
因此,一般来说:
在大多数情况下,在处理应实现目标接口的自定义视图或视图持有者时,应使用此选项
但是:
在您的情况下,我认为最好的解决方案实际上是事先创建/查找图像视图
,并让毕加索将图像直接加载到其中