Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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!