简体   繁体   中英

Images are not loading from the Firebase Storage

I'm using Firebase Storage's new method listAll(); to list all the images from my storage to my app but the images are not loading into my app and shows a empty screen. I don't know what causes this error.

My Activity Class:

public class Events_and_Participations extends AppCompatActivity {

    StorageReference reference;
    RecyclerView recyclerView;
    Images_Adapter adapter;
    List<Task<Uri>> items = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_events_and_participations);
        Objects.requireNonNull(getSupportActionBar()).setTitle(R.string.item_title_4);

        reference = FirebaseStorage.getInstance().getReference("Events & Participations");

        reference.listAll()
                .addOnSuccessListener(this, listResult -> {

                    for (StorageReference item : listResult.getItems()) {
                        items.add(item.getDownloadUrl());
                    }
                })
                .addOnFailureListener(e -> Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show());

        adapter = new Images_Adapter(items);
        recyclerView = findViewById(R.id.events_photos);
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
}

My Image Adapter class:

public class Images_Adapter extends RecyclerView.Adapter<Images_Adapter.Images_VH> {

    private List<Task<Uri>> items;

    Images_Adapter(List<Task<Uri>> items) {
        this.items = items;
    }

    @NonNull
    @Override
    public Images_VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_events_image, parent, false);
        return new Images_VH(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Images_VH holder, int position) {
        Task<Uri> item = items.get(position);
        Picasso.get()
                .load(item.getResult())
                .resize(250,250)
                .centerCrop()
                .into(holder.imageView);
    }

    @Override
    public int getItemCount() {
        return items.size();
    }

    static class Images_VH extends RecyclerView.ViewHolder{
        ImageView imageView;

        Images_VH(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.ImageItem);
        }
    }
}

These are my firebase storage rules:

rules_version = '2';
service firebase.storage {
  match /b/sahrudhaya-foundation.appspot.com/o {
    match /{allPaths=**} {
      allow read, write :if request.auth != null;
    }
  }
}

It throws error kind of "users does not have permission to access the items"

I'm totally confused and this was my first trail for this. Hoping for a little help on this.

Actually, I'm answering my own question cause someone in future will find it useful.

If anyone wants to load the images without authentication the change the security rules for that particular folder to public. For example say you have a folder named images which contains all the images now go to your rules in storage and write

rules_version = '2';
service firebase.storage {
  match /b/sahrudhaya-foundation.appspot.com/o {
    match /{allPaths=**} {
      
       match /images/{allPaths=**} {
          allow read, write: if true;
       }
    }
  }
}

then that particular folder will be public so you can access all the images without authentication.

And also if you want to get the download links then go with this code.

List<Strings> items = new ArrayList();

reference = FirebaseStorage.getInstance().getReference("YOUR_FOLDER_NAME");
    
            reference.listAll()
                    .addOnSuccessListener(this, listResult -> {
    
                        for (StorageReference item : listResult.getItems()) {
                            items.clear(); //To clear all the previous stored strings. 
                            item.getDownloadUrl().addOnCompleteListener(task -> {
                            items.add(task.getResult()).toString());
                        });
                           
                        }
                    })
                    .addOnFailureListener(e -> Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show());

Try to replace request.auth!=null with auth.=null`. See if that works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM