0

I've got GridView adapter and I want to download photos from Firebase Storage via photo's URL.

When I am passing the URL to the GridViewAdapter the photo is blank. There is space but there is no picture. Like the transparent picture.

If I pass the images' URL found in Google it works, but it does not work with Firebase Storage's URL. What is wrong?

This is how I try to get URL from Firebase Storage I am getting DataSnapshot of node which contains all URLs

databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                photos.clear();
                for(DataSnapshot ds: dataSnapshot.getChildren()){
                    String photoURL = ds.getValue(String.class);
                    try {
                        photos.add(photoURL);


                    }catch (NullPointerException e) {
                    }
                }
                gridViewAdapter = new GridViewAdapter(getContext(), photos);
                gridView.setAdapter(gridViewAdapter);

            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                }
        });

my GridViewAdapter.class, getView method

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View gridView = convertView;

        if(convertView == null){

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            gridView = inflater.inflate(R.layout.image_item,null);

        }

        ImageView imageView = gridView.findViewById(R.id.image_from_photo_album);

        Picasso.with(context).load(photos.get(position)).into(imageView);

        return imageView;
    }

ScreenView without Firebase Storage URL:

ScreenView without Firebase Storage URL

ScreenView with Firebase Storage URL:

ScreenView with Firebase Storage URL

skomisa
  • 16,436
  • 7
  • 61
  • 102
Coderian
  • 23
  • 6
  • did you debug the url ? And than try to see in i. a browser if it is correct? – coroutineDispatcher Apr 16 '19 at 22:01
  • Yes, i did it. It is saved correctly in my Firebase Database. If I copy the URL directly from image directory in Firebase Database and paste it to Picasso it gives me the same result. – Coderian Apr 16 '19 at 22:32
  • I recommend you to change Picasso to Glide ))) I had many problems with Picasso library, and Glide solved it quickly. Maybe picasso have some problems with cache, or downloaded with error. – Scrobot Apr 17 '19 at 05:31

4 Answers4

1

Try to change Picasso.with(context).load(photos.get(position)).into(imageView) into : String url = photos.get(position); Picasso.with(context).load(url).into(imageView)

Also : maybe your photo url comming from Firebase is always Null , have you ever debuged it ?

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
  • I tried it. It changed nothing like I supposed. The url is ok. If I paste the same URL into web browser it works if I paste it into my code it does not. – Coderian Apr 16 '19 at 22:33
0

This is very broad. When you "pass" the url to your GridViewAdapter how do you load the image? Maybe upload your code so we can help you.

  • uploaded. I pass the URL with Picasso. – Coderian Apr 16 '19 at 20:38
  • First what i would do it make sure your photos is getting the url. Thrrow a break point on String photoURL = ds.getValue(String.class); – LinuxMasterRace Apr 16 '19 at 20:47
  • It is getting the url. I see it in the Firebase Realtime Database and in the ToastMessage. If I paste the URL straight from Firebase Storage into Firebase Database it is the same and if I paste it into Picasso path it is the same too. Blank image. – Coderian Apr 16 '19 at 20:53
  • I'm not completely sure what the issue is then. if you have the url and are passing to the adapter. one thing i will say is why are you initializing your gridview adapter on datachange()? Put that in your onCreate and call gridviewadapter.notifydatasetchanged where it was at – LinuxMasterRace Apr 16 '19 at 21:07
  • Thats your problem. Call gridViewAdapter.notifydatasetchanged(); – LinuxMasterRace Apr 16 '19 at 21:09
  • I moved gridViewAdapter initializaition outside and I added gridViewAdapter.notifyDataSetChanged(). It works like it worked earlier. Still blank space instead of picture. – Coderian Apr 16 '19 at 21:17
  • So just to be clear. You moved the adapter to onCreate and you added adapter.notifydatasetchanged(); right outside of your for loop in onDataChange? – LinuxMasterRace Apr 16 '19 at 21:29
  • Yes, I did that. – Coderian Apr 16 '19 at 22:25
  • Try putting a toast in your catch statement or debug it. Toast.makeText(this, "Error:" + e.getMessage(), Toast.LENGTH_SHORT).show(); – LinuxMasterRace Apr 16 '19 at 22:34
0

If your URL correct, ImageView render another images correctly, then it seems that Picasso produced some possible problems:

  • You have problems with caching or with downloader. See this topic How do I use disk caching in Picasso? maybe it help you.
  • Downloading of your URL completing with error. Try to add error handling to picasso chain
Picasso.with(context)
    .load(photos.get(position))
    .error(R.drawable.error_image)
    .placeholder(R.mipmap.ic_launcher)
    .into(imageView);

more details here

Scrobot
  • 1,911
  • 3
  • 19
  • 36
0

The problem was, that I had set wrap_content attribute on layout_width and layout_height in ImageView that was responsible for displaying the image. Earlier the height of this image was very very big (I don't know why) and it was causing the problem.

Note: GridView for images' url from Google with resolution 800x450 works perfectly, the problem is only with photos from Firebase Storage.

Problem is partially solved, because if I set fixed size of image it display the image but I still have some issues... the GridView for example sometimes layers the two images on the same position, or the first elements of GridView aren't loading.

Coderian
  • 23
  • 6