0

I've just imported the Picasso library in my project and I'm using it like described in this other SO question (I need to download & save some images from web server). The whole thing is working correctly except for a thing: the resulting image in my emulator is bigger (in terms of weight) than the original on server. For example, a JPG image of 52Kb (370 x 505 pixels), result in a JPG image of same resolution (370 x 505) but 132Kb of weight. This is the code I'm using (saw in a lot of SO questions):

if (target == null){
    target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(getActivity().getFilesDir() + "/saved.jpg");
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                        ostream.flush();
                        ostream.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {}

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {}
    };
}
Picasso.with(getActivity()).load(sourceValue).into(target);

Thanks you all in advance for your time and your attention.

UPDATE:

Checking the memory of my emulator, I've noticed that the cached version of the image that Picasso create, has the correct size. So, basically, if I use the same code I've posted, but I comment all the try-catch block, the file is just downloaded and cached (not saved in another location) but has the correct size. So it is compression's fault?

MatteoBelfiori
  • 280
  • 3
  • 13
  • What's your question? – Marcus Apr 01 '15 at 18:26
  • You may request a .jpg but as you see Volley delivers you a Bitmap. So the resulting image is a Bitmap which exist of much more bytes than the original .jpg file. Then you are using BitmapFactory to compress the bitmap to a .jpg. Apparently the conversion from bitmap to jpg is not the reverse as conversion from jpg to bitmap. So if you want to download your .jpg unharmed than this is not the way to go. – greenapps Apr 01 '15 at 18:44
  • @Marcus My question is: why the final image has a weight greater than the original one? So in what way I'm supposed to download the image? I've done in this way because it's the first time I use Picasso, and I saw a couple of articles that show how to download images with it and they do in this way. – MatteoBelfiori Apr 01 '15 at 19:20
  • Added an update to original post – MatteoBelfiori Apr 02 '15 at 14:05

0 Answers0