How to get a list of all files in Cloud Storage in a Firebase app?

后端 未结 19 1034
傲寒
傲寒 2020-11-21 11:25

I\'m working on uploading images, everything works great, but I have 100 pictures and I would like to show all of them in my View, as I get the complete list of

19条回答
  •  终归单人心
    2020-11-21 11:51

    I also encountered this problem when I was working on my project. I really wish they provide an end api method. Anyway, This is how I did it: When you are uploading an image to Firebase storage, create an Object and pass this object to Firebase database at the same time. This object contains the download URI of the image.

    trailsRef.putFile(file).addOnSuccessListener(new OnSuccessListener() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Uri downloadUri = taskSnapshot.getDownloadUrl();
                DatabaseReference myRef = database.getReference().child("trails").child(trail.getUnique_id()).push();
                Image img = new Image(trail.getUnique_id(), downloadUri.toString());
                myRef.setValue(img);
            }
        });
    

    Later when you want to download images from a folder, you simply iterate through files under that folder. This folder has the same name as the "folder" in Firebase storage, but you can name them however you want to. I put them in separate thread.

     @Override
    protected List doInBackground(Trail... params) {
    
        String trialId = params[0].getUnique_id();
        mDatabase = FirebaseDatabase.getInstance().getReference();
        mDatabase.child("trails").child(trialId).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                images = new ArrayList<>();
                Iterator iter = dataSnapshot.getChildren().iterator();
                while (iter.hasNext()) {
                    Image img = iter.next().getValue(Image.class);
                    images.add(img);
                }
                isFinished = true;
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        });
    

    Now I have a list of objects containing the URIs to each image, I can do whatever I want to do with them. To load them into imageView, I created another thread.

        @Override
    protected List doInBackground(List... params) {
    
        List bitmaps = new ArrayList<>();
    
        for (int i = 0; i < params[0].size(); i++) {
            try {
                URL url = new URL(params[0].get(i).getImgUrl());
                Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                bitmaps.add(bmp);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        return bitmaps;
    }
    

    This returns a list of Bitmap, when it finishes I simply attach them to ImageView in the main activity. Below methods are @Override because I have interfaces created and listen for completion in other threads.

        @Override
    public void processFinishForBitmap(List bitmaps) {
        List imageViews = new ArrayList<>();
        View v;
        for (int i = 0; i < bitmaps.size(); i++) {
            v = mInflater.inflate(R.layout.gallery_item, mGallery, false);
            imageViews.add((ImageView) v.findViewById(R.id.id_index_gallery_item_image));
            imageViews.get(i).setImageBitmap(bitmaps.get(i));
            mGallery.addView(v);
        }
    }
    

    Note that I have to wait for List Image to be returned first and then call thread to work on List Bitmap. In this case, Image contains the URI.

        @Override
    public void processFinish(List results) {
        Log.e(TAG, "get back " + results.size());
    
        LoadImageFromUrlTask loadImageFromUrlTask =  new LoadImageFromUrlTask();
        loadImageFromUrlTask.delegate = this;
        loadImageFromUrlTask.execute(results);
    }
    

    Hopefully someone finds it helpful. It will also serve as a guild line for myself in the future too.

提交回复
热议问题