场景:我想从服务器下载一些图像并将它们存储到一个文件中.我知道如何存储到文件和检索.当我运行下面的代码时,我碰巧看到我只得到最后一张图片.看起来Picasso并行运行.我通过显示一个Toast消息来检查它.反正有没有解决这个问题?
问题:我只获得最后一个网址图片.
这是我的代码
static int count = 0;@OverrIDeprotected 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,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 "";}解决方法 试试这种方式,希望这可以帮助您解决问题.
static int count = 0;@OverrIDeprotected 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,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(),which always display as **2**. return "";}总结
以上是内存溢出为你收集整理的在Android中从Picasso下载多个图像全部内容,希望文章能够帮你解决在Android中从Picasso下载多个图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)