问题与List();由Firebase存储功能

问题描述 投票:0回答:1

我正在尝试将Firebase存储中的所有图像列出到应用程序中。我知道将图像添加到存储并添加到数据库的链接的方法,但是我的客户端不知道这一点。因此,当我发现Firebase当前允许列出文件夹中的所有图像时。最近两天我一直在努力,但是没有运气。

我的活动代码:

public class Events_and_Participations extends AppCompatActivity {

    StorageReference reference;
    RecyclerView recyclerView;
    Images_Adapter adapter;
    List<String> items = new ArrayList<>();
    FirebaseAuth auth;
    Button button;
    FirebaseUser user;

    @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);

        button = findViewById(R.id.login);

        auth = FirebaseAuth.getInstance();
        user = FirebaseAuth.getInstance().getCurrentUser();

        if (user == null){
            button.setVisibility(View.VISIBLE);
        }else {
            button.setVisibility(View.INVISIBLE);
        }
        reference = FirebaseStorage.getInstance().getReference("images");

        button.setOnClickListener(v -> auth.signInAnonymously().addOnCompleteListener(task -> {
            if (task.isSuccessful()){
                Toast.makeText(this, "Loading..!!", Toast.LENGTH_SHORT).show();
            }
        }));

        if (user != null){
            reference.listAll()
                    .addOnSuccessListener(listResult -> {

                        for (StorageReference item : listResult.getItems()) {
                            items.add(item.getDownloadUrl().toString());
                        }
                    })
                    .addOnFailureListener(e -> Toast.makeText(Events_and_Participations.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 LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }
}

我的存储规则:

rules_version = '2';
service firebase.storage {
  match /b/sahrudhaya-foundation.appspot.com/o {
    match /{allPaths=**} {
      allow read, write : if request.auth != null
    }
  }
}
java android firebase firebase-storage
1个回答
0
投票

getDownloadUrl不会直接返回Uri,而是会返回Task<Uri>。您必须添加一个回调,并使用addOnSuccessListeneraddOnCompleteListener来通知其实际值(就像您对列表本身所做的一样)

© www.soinside.com 2019 - 2024. All rights reserved.