1

I am trying to download and save images inside the getView() function of an adapter using a Picasso target. I have a url which points to an image. This image gets saved on the phone. If the image has already been saved, I retrieve it from the phones memory and display, otherwise I use a Picasso Target to save the bitmap into the phone's storage. My problem is, the target is never entering the onBitmapLoaded function, it only goes to onBitmapFailed. Therefore my bitmap is never being saved to phone storage, since the saving happens in the onBitMapLoaded function.

The code below is the getView() function, inside the try catch block towards the bottom it checks to see if the file is already saved on disc. If it is, I try to decode the file as a Bitmap, and display it into an imageView. Otherwise, I use a Picasso Target to download the image.

       @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            // Get or create the feedback wrapper to allow click feedback
            LinearLayoutFeedback feedbackWrapper;
            final ImageView imageView;

            // First try to reuse previous views if available
            if (convertView != null) {
                feedbackWrapper = (LinearLayoutFeedback) convertView;
                imageView = (ImageView) feedbackWrapper.getChildAt(0);
            }

            // If not create new views
            else {
                feedbackWrapper = new LinearLayoutFeedback(viewGroup.getContext());
                imageView = new ImageView(viewGroup.getContext());
                imageView.setLayoutParams(new ViewGroup.LayoutParams(PHOTOSTREAM_PICTURE_WIDTH, PHOTOSTREAM_PICTURE_HEIGHT));
                imageView.setPadding(0, 0, 0, 0);//padding for image tiles
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                //imageView.setBackgroundColor(getResources().colorForClass(R.color.pstream_black));
                feedbackWrapper.addView(imageView);
            }

            int circularPosition = getCircularPosition(position);
            Photo photo = getPhotoData().get(circularPosition);

            //Below line was commented out, and instead of using an ImageRequest to load into ImageView, we just use Picasso, it's easier and, handles
            //errors better
//            loadImage(photo.imageMed, imageView);

            final String url = photo.imageMed;
            try{
                String fileName = Environment.getExternalStorageDirectory().getPath() + "/" + url;
                File pic = new File(fileName);
                    if(pic.exists()){
                        Bitmap bitmap = BitmapFactory.decodeFile(fileName);
                        imageView.setImageBitmap(bitmap);
                    }else{
//                    loadImageWithPicasso(photo.imageMed, imageView);
                        final Target target = getTarget(url, imageView);
                        Picasso.with(getContext()).load(url).into(target);
                        imageView.setTag(target);
                }
            }catch (Exception e ){
                e.printStackTrace();
            }
            return feedbackWrapper;
        }

Below is the code for getTarget(String url, ImageView imageView)

Target getTarget(final String url, final ImageView imageView){
    Target target = new Target(){
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            imageView.setImageBitmap(bitmap);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    File file = new File(Environment.getExternalStorageDirectory().getPath() + "/" + url);
                    try {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
                        ostream.flush();
                        ostream.close();
                    } catch (IOException e) {
                        Log.e("IOException", e.getLocalizedMessage());
                    }
                }
            }).start();
        }
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.art_white));                }
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            imageView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.art_white));                }
    };
    return target;
}

I don't understand why the getTarget function is never entering onBitmapLoaded. I also don't understand why no images are being displayed inside the imageView with this code. Any help would be greatly appreciated.

Ben Holmes
  • 11
  • 3

1 Answers1

0

The code above works. Turns out wifi was down, so I was never receiving the image from the URL... Really sucks to have wasted so much time on an issue like that. The only part of the code which is still wrong is the part where it tries to save the bitmap to the SD card.. Still trying to figure out what I'm doing wrong there, but when I do, I will post my solution.

Ben Holmes
  • 11
  • 3