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

Loading and saving images into file on sd card #1139

Closed
ViHtarb opened this issue Sep 14, 2015 · 6 comments
Closed

Loading and saving images into file on sd card #1139

ViHtarb opened this issue Sep 14, 2015 · 6 comments

Comments

@ViHtarb
Copy link

ViHtarb commented Sep 14, 2015

How make loading and saving images into file?
I try use this method:

private Target target = new Target() {
    @Override
    public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
        File file = new File(Environment.getExternalStorageDirectory().getPath()  + "/saved.jpg");
        try {
            file.createNewFile();
            FileOutputStream ostream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
            ostream.close();
         } catch (Exception e) {
            e.printStackTrace();
         }
    }

    @Override
    public void onBitmapFailed(Drawable errorDrawable) {}

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

@Override
public void onClick(View v) {
    Picasso.with(context).load("url").into(target);
}

On first click calling only onPrepareLoad method.
Method onBitmapLoaded calling only on second and more clicks.

I need to calling onBitmapLoaded method on first click.

PS. images for loading in HD size;
PSS. if loading smaller images all wonderful

@luccasmaso
Copy link

You can simplify and let the OkHttpDownloader from Picasso to handle caching images to disk. Picasso supports disk caching through it's HTTP client.

For example:

mBuilder = new Picasso.Builder(context) 
                  .loggingEnabled(BuildConfig.DEBUG) 
                  .indicatorsEnabled(BuildConfig.DEBUG) 
                  .downloader(new OkHttpDownloader(context, DISK_CACHE_SIZE)) 
                  .build();

mBuilder.load("url").into(target);

@LikeWather
Copy link

<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />

have this???

@jzeferino
Copy link

@luccasmaso using the line new OkHttpDownloader(context, DISK_CACHE_SIZE) will cache all downloaded images to disk?

@luccasmaso
Copy link

@altaf933
Copy link

I have implemented this function by a interface after downloaded successfully than it will callback into my class.

@JakeWharton
Copy link
Collaborator

This seems like a dupe of #506.

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

6 participants