我正在使用Firebase Storage的新方法listAll();
列出存储中所有图像到我的应用程序,但是这些图像未加载到我的应用程序中,并显示空白屏幕。我不知道是什么原因导致此错误。
我的活动分类:
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());
}
}
我的图像适配器类:
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);
}
}
}
这些是我的Firebase存储规则:
rules_version = '2';
service firebase.storage {
match /b/sahrudhaya-foundation.appspot.com/o {
match /{allPaths=**} {
allow read, write :if request.auth != null;
}
}
}
会引发错误类型的“用户无权访问项目”
我完全感到困惑,这是我的第一步。希望对此有所帮助。
尝试替换request.auth!=null with
auth!= null`。看看是否可行。