我正在使用Picasso库从URL下载图片。这是我第一次尝试使用Picasso。
场景:我想从服务器下载一些图片并将它们存储到文件中。我知道如何存储到文件和检索。当我运行下面的代码时,我发现我只得到最后一个图片。似乎Picasso是并行运行的。我通过显示toast消息来检查它。有没有办法解决这个问题?
问题:我只得到了最后一个url图片。
这是我的代码:
场景:我想从服务器下载一些图片并将它们存储到文件中。我知道如何存储到文件和检索。当我运行下面的代码时,我发现我只得到最后一个图片。似乎Picasso是并行运行的。我通过显示toast消息来检查它。有没有办法解决这个问题?
问题:我只得到了最后一个url图片。
这是我的代码:
static int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < url.length; i++)
{
// url is String array which has 2 urls.
++count; // Incrementing the count by 1
Picasso.with(this).load(url[i])
.into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
// TODO Auto-generated method stub
}
@Override
public void onBitmapLoaded(Bitmap arg0, LoadedFrom arg1) {
// TODO Auto-generated method stub
arg0 = Bitmap.createScaledBitmap(arg0, 150, 150, true);
filePath = saveFile(arg0); // I'm just calling this function to check how many times `onBitmapLoaded` is called. And it is called only once...!!
}
@Override
public void onBitmapFailed(Drawable arg0) {
// TODO Auto-generated method stub
}
});
}
}
public String saveFile (Bitmap bm)
{
Toast.makeText(getApplicationContext(), ""+count, 100).show(); // Displaying the value of count, which always display as **2**.
return "";
}