毕加索在Android的下载倍数图像 [英] Downloading multiples images from Picasso in Android

查看:145
本文介绍了毕加索在Android的下载倍数图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是毕加索库从网址下载图像。这是我对毕加索第一次尝试

方案:我想从服务器上下载一些图片,并将它们存储到文件中。我知道如何保存到文件和检索。当我跑了低于code,我碰巧看到,我只获得最后一张图像。它看起来像毕加索平行运行。我通过显示敬酒消息,检查了它。反正是有解决这个问题?

问题:我只得到最后的URL图像。

下面是我的code

 静态诠释计数= 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    的for(int i = 0; I< url.length;我++)
    {        // URL是String数组,其中有2个网址。
        ++计数; //递增1计数
        Picasso.with(本).load(URL [I])
        .into(新目标(){            @覆盖
            在prepareLoad(绘制对象为arg0)公共无效{
                // TODO自动生成方法存根            }            @覆盖
            公共无效onBitmapLoaded(位图将arg0,ARG1 LoadedFrom){
                // TODO自动生成方法存根
                将arg0 = Bitmap.createScaledBitmap(为arg0,150,150,TRUE);
                文件路径= saveFile的(为arg0); //我只是调用这个函数来检查onBitmapLoaded`多少次`调用。它被称为只有一次......!
            }            @覆盖
            公共无效onBitmapFailed(绘制对象为arg0){
                // TODO自动生成方法存根            }
        });
    }
}公共字符串了saveFile(位图BM)
{   Toast.makeText(getApplicationContext(),+计数,100).show(); //显示计数的值,它始终显示为** 2 **。
   返回;
}


解决方案

试试这个办法,希望这将帮助你解决你的问题。

 静态诠释计数= 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    的for(int i = 0; I< url.length;我++)
    {        // URL是String数组,其中有2个网址。
        Picasso.with(本).load(URL [I])
        .into(新目标(){            @覆盖
            在prepareLoad(绘制对象为arg0)公共无效{
                // TODO自动生成方法存根            }            @覆盖
            公共无效onBitmapLoaded(位图将arg0,ARG1 LoadedFrom){
                // TODO自动生成方法存根
                将arg0 = Bitmap.createScaledBitmap(为arg0,150,150,TRUE);
                ++计数; //递增1计数
                文件路径= saveFile的(为arg0); //我只是调用这个函数来检查onBitmapLoaded`多少次`调用。它被称为只有一次......!
            }            @覆盖
            公共无效onBitmapFailed(绘制对象为arg0){
                // TODO自动生成方法存根            }
        });
    }
}公共字符串了saveFile(位图BM)
{   Toast.makeText(getApplicationContext(),+计数,100).show(); //显示计数的值,它始终显示为** 2 **。
   返回;
}

I'm using Picasso library to download images from URL. This is my first attempt on Picasso

Scenario : I want to download some images from server and store them into a file. I know how to store into file and retrieve. When I ran the below code, I happen to see that I'm getting only last image. It seems like Picasso runs parallelly. I checked it by displaying a toast message. Is there anyway to solve this issue?

Problem : I'm getting only the last url image.

Here's my code

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

解决方案

Try this way,hope this will help you to solve your problem.

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. 
        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);
                ++count; // Incrementing the count by 1
                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 "";
}

这篇关于毕加索在Android的下载倍数图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆