Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download multiple images and store in storage #827

Closed
FarshadKazemi opened this issue Jan 3, 2015 · 4 comments
Closed

Download multiple images and store in storage #827

FarshadKazemi opened this issue Jan 3, 2015 · 4 comments

Comments

@FarshadKazemi
Copy link

I'm going to download images from the internet and save them in storage at the same time.
I know that I can save one image through calling Target class.
I use below code to download my images, but it just downloads one image.
Any suggestion would be appreciated ...

@Override
 protected void onPostExecute(Void result) {
SaveImages();
pDialog.dismiss();
super.onPostExecute(result);
}
}

String fileName = null;
public void SaveImages()
{
for(int i=0; i<image.length; i++)
{
    Picasso.with(this).load(image[i]).into(target);
    fileName = "image-" + i + ".jpg";
}
}

Target target = new Target() {

@Override
public void onPrepareLoad(Drawable arg0) {
    // TODO Auto-generated method stub

}

@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {

    File file = new File(Environment.getExternalStorageDirectory().getPath() +"/" + fileName);
     try 
     {
     file.createNewFile();
     FileOutputStream ostream = new FileOutputStream(file);
     bitmap.compress(CompressFormat.JPEG, 75, ostream);
     ostream.close();
     } 
     catch (Exception e) 
     {
     e.printStackTrace();
     }
}

@Override
public void onBitmapFailed(Drawable arg0) {
    // TODO Auto-generated method stub

}
};
@JakeWharton
Copy link
Collaborator

Dupe of #506.

@Baraka1Ahmad
Copy link

Baraka1Ahmad commented Feb 4, 2018

Hello my friend
I encountered the same problem, you need a thread

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;
    private String mURL = "http://www.allindiaflorist.com/imgs/arrangemen4.jpg";
    private String urls = "http://api.androidhive.info/images/sample.jpg";
    private String[] urlArray = {mURL, urls};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.imageViewID);

    }

    @Override
    protected void onResume() {
        super.onResume();

        Picasso.with(getApplicationContext()).load(mURL).into(mImageView);

        for (int i=0; i<urlArray.length; i++) {
            Picasso.with(getApplicationContext()).load(urlArray[i]).into(target);

        }

    }

    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {

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

                    File sd = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
                    File folder = new File(sd, "/Picasso/");
                    if (!folder.exists()) {
                        if (!folder.mkdir()) {
                            Log.e("ERROR", "Cannot create a directory!");
                        } else {
                            folder.mkdir();
                        }
                    }

                    File[] fileName = {new File(folder, "one.jpg"), new File(folder, "two.jpg")};

                    for (int i=0; i<fileName.length; i++) {

                        if (!fileName[i].exists()) {
                            try {
                                fileName[i].createNewFile();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {

                            try {
                                FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName[i]));
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                                outputStream.close();

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

                    }

                }
            }).start();

        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

}

@pnitish16
Copy link

its loading single bitmap in all the files :(

@revdilip
Copy link

@Barakahamad1 here you have named the file one and two, can we get the name there from url .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants