Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

aakarshrestha
aakarshrestha
6,509 Points

How to save multiple image files in the SD card using picasso?

Here is my code. Any help is appreciated!!! I want to write both files that will be stored in the SD card the correct image in both. But it is not working as expected.

    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) {

            }
        };

    }

1 Answer

aakarshrestha
aakarshrestha
6,509 Points

Fixed it!

Now i can save several images from internet to my internal or external storage.

happy coding!