Hassam Talha
Hassam Talha

Reputation: 57

How to Download Images through URL to internal memory?

I want to Download my images which i have uploaded to Firebase. I Got the URL and was able to load them Directly to my desired ImageView. But I want to download them to my desired path in internal Storage.

I Have Used Picasso But its not working for me as i am getting the error attached below when Using ( new Target() ) This is the Error i got using Picasso with Target

enter image description here

And the Code For Picasso is Below

Picasso.get()
                .load(downloadUrl)
                .into(new Target() {
                          @Override
                          public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                              try {

                                  String root = Environment.getExternalStorageDirectory().toString();
                                  File myDir = new File(root + "/yourDirectory");

                                  if (!myDir.exists()) {
                                      myDir.mkdirs();
                                  }

                                  String name = new Date().toString() + ".jpg";
                                  myDir = new File(myDir, name);
                                  FileOutputStream out = new FileOutputStream(myDir);
                                  bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

                                  out.flush();
                                  out.close();
                              } catch(Exception e){
                                  // some action
                              }
                          }

                          @Override
                          public void onBitmapFailed(Drawable errorDrawable) {

                          //Some Action;
                          }

                          @Override
                          public void onPrepareLoad(Drawable placeHolderDrawable) {
                            //Some Action;
                          }
                      }
                );

I have Searched in the Documentation But that Didn't Work. Please Let me Know As soon as possible. If someone have the Right Answer. Thanks in Advance

Upvotes: 1

Views: 589

Answers (2)

Jitesh Prajapati
Jitesh Prajapati

Reputation: 2533

Note: To remove error, you have to implement methods of Target class.

For Download Image, You can find solution from the link

https://www.codexpedia.com/android/android-download-and-save-image-through-picasso/

Picasso.with(this).load(anImageUrl).into(picassoImageTarget(getApplicationContext(), "imageDir", "my_image.jpeg"));

method

private Target picassoImageTarget(Context context, final String
imageDir, final String imageName) {
    Log.d("picassoImageTarget", " picassoImageTarget");
    ContextWrapper cw = new ContextWrapper(context);
    final File directory = cw.getDir(imageDir, Context.MODE_PRIVATE); // path to /data/data/yourapp/app_imageDir
    return new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final File myImageFile = new File(directory, imageName); // Create image file
                    FileOutputStream fos = null;
                    try {
                        fos = new FileOutputStream(myImageFile);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                   Log.i("image", "image saved to >>>" + myImageFile.getAbsolutePath());

                }
            }).start();
        }

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

Upvotes: 0

Amrdroid
Amrdroid

Reputation: 387

first: implement Taget class methods to clear the error

second:your code throws this exception java.io.FileNotFoundException because the default format of Date object is like that Mon Jan 21 12:10:23 GMT+02:00 2019 and you can't create file with name contains : colon

third: check if you add android.permission.WRITE_EXTERNAL_STORAGE Permission in androidManiefest.xml and you need to use runtime permission in marshmallow

Upvotes: 1

Related Questions