Francisco Melicias Francisco Melicias - 5 months ago 15
Android Question

Just go to the target once and it should go 4 times. Picasso/Target

So I implement picasso, so I can download images and save them on sd cart, so when I want i can use them on the program.

I have a

for
loop:

for (int i = 0; i < listaProdutos.size(); i++) {
caminho =listaProdutos.get(i).getImagem();
Picasso.with(getApplicationContext()).load("URL"+listaProdutos.get(i).getImagem()).into(target);
}


But I just get into target once and its the last one of for loop,

target code:

private Target target = new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
//new Thread(new Runnable() {
//@Override
//public void run() {
/*
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(),caminho);
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 75, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}*/
try {
verifyStoragePermissions(AtividadePrincipal.this);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/imagensDaApp");
myDir = new File(myDir, caminho);
if (!myDir.exists()) {
myDir.getParentFile().mkdirs();
//myDir.createNewFile();
}
FileOutputStream out = null;
out = new FileOutputStream(myDir);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//}
//}).start();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}

@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
if (placeHolderDrawable != null) {
}
}
};


I commented the thread because it does the same thing with or without, if you want you cant uncomment.

I have alredy search about it, but i couldnt find any answer for this problem, all the URL are ok!

I have been been doing this for 3 days, and it keep the same.

YES listaProdutos.size() = 4;
AND all the url are ok!


If u didnt understand the question, please say.

Answer

Why not use the following code? No Targets are used here, so no target is getting gc'ed

I took the freedom to optimize your code a little as well. I've done this without testing the code, but it should work without any problems.

    new Thread(new Runnable(){
              @Override
              public void run(){

                for (int i = 0; i < listaProdutos.size(); i++) {
                                caminho =listaProdutos.get(i).getImagem();

                                try {
                                    verifyStoragePermissions(AtividadePrincipal.this);
                                    String root = Environment.getExternalStorageDirectory().toString();
                                    File myDir = new File(root + "/imagensDaApp");
                                    myDir = new File(myDir, caminho);
                                    if (!myDir.exists()) {
                                        if(myDir.getParentFile().mkdirs()){
                                           //myDir.createNewFile();
                                           FileOutputStream out = null;
                                           out = new FileOutputStream(myDir);
                                           Picasso.with(getApplicationContext()).load("URL"+listaProdutos.get(i).getImagem()).get().compress(Bitmap.CompressFormat.JPEG, 90, out);

                                           out.flush();
                                           out.close();
                                        }

                                    }

                                } catch (FileNotFoundException e){
                                    e.printStackTrace();
                                }catch{IOException  e) {
                                    e.printStackTrace();
                                }

                            }
              }
            }).start();