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 Image to Strorage #1299

Closed
amebrahimi opened this issue Feb 20, 2016 · 2 comments
Closed

Download Image to Strorage #1299

amebrahimi opened this issue Feb 20, 2016 · 2 comments

Comments

@amebrahimi
Copy link

Hi , I'm using your wonderful library but i have a problem with it , the problem is i want to download a picture from a URL and save it to the storage and i'm using this code

 Picasso.with(mContext)
            .load("http://apod.nasa.gov/apod/image/1602/IMG_0193PorterAstroH.jpg")
            .into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    Log.d(TAG, "onBitmapLoaded is launched");
                    try {
                        String root = Environment.getExternalStorageDirectory().toString();
                        File myDir = new File(root + "/Image Capture");
                        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){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                    Log.d(MainActivity.TAG, "Failed :(((" );
                }
                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {}
            });

inside a button and when i press the button i see that its using the network to download the picture but as you see my loges don't show anything to me after the Target() Interface .

Sometimes it creates the folder and downloads the picture to my storage but usually it doesn't.

@roomtek
Copy link

roomtek commented Feb 20, 2016

I don't think Picasso is to be used as a download library. If you are not displaying the "bitmap" why don't you just stream the bytes from the URL to your file.?

@JakeWharton
Copy link
Member

Calling new Target will cause the target to be garbage collected immediately (and thus the code never run) since Picasso keeps a weak reference. Put your target in a field or somewhere else where a strong reference can be kept to prevent this and allow the code to work.

Other than that, this is a dupe of #506 which we just haven't gotten around to yet.

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

3 participants