Hola, quiero obtener imágenes de Firebase a la memoria. Cuando deprimo mi código, devuelve la vista primero, luego vuelve a usersPhotoRef.addValueEventListener() e imprime el tamaño mPhotoArrayList como 0. Finalmente vuelve a entrar en photoRef.getBytes()#addOnSuccessListener() y pone el mapa de bits en la lista de array. Creo que mi código debe corregirse para que pueda pasar mPhotoArrayList en el adaptador. ¿Cómo puedo hacer esto o qué cambios deben hacerse?

Salida de este código:

2019-06-30 15:01:00.130 12154-12154/com.newstrange.worklog E/RecyclerView: No adapter attached; skipping layout
2019-06-30 15:01:00.134 12154-12158/com.newstrange.worklog I/art: Do partial code cache collection, code=24KB, data=30KB
2019-06-30 15:01:00.135 12154-12158/com.newstrange.worklog I/art: After code cache collection, code=23KB, data=30KB
2019-06-30 15:01:00.135 12154-12158/com.newstrange.worklog I/art: Increasing code cache capacity to 128KB
2019-06-30 15:01:00.151 12154-12154/com.newstrange.worklog I/IMAGE_NAME: asdasd_giris_25.54.47_29.Mayıs.2019
2019-06-30 15:01:00.169 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.169 12154-12154/com.newstrange.worklog I/IMAGE_NAME: fdsafdas_giris_25.55.4_29.Mayıs.2019
2019-06-30 15:01:00.170 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.170 12154-12154/com.newstrange.worklog I/IMAGE_NAME: sdf_cikis_8.44.32_30.Mayıs.2019
2019-06-30 15:01:00.173 12154-12154/com.newstrange.worklog I/SELFIES: 0
2019-06-30 15:01:00.842 12154-12154/com.newstrange.worklog I/GET_BITMAP: 1
2019-06-30 15:01:00.846 12154-12154/com.newstrange.worklog I/GET_BITMAP: 2
2019-06-30 15:01:00.858 12154-12154/com.newstrange.worklog I/GET_BITMAP: 3

El código:

package com.newstrange.worklog;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import java.io.IOException;
import java.util.ArrayList;

public class ListSelfiesFragment extends Fragment {

    private FirebaseStorage mStorageIns;
    private FirebaseDatabase mDatabaseIns;
    private RecyclerView mRecyclerView;
    private GridLayoutManager mLayoutManager;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mDatabaseIns = FirebaseDatabase.getInstance(); // DATABASE

        mStorageIns = FirebaseStorage.getInstance(); // STORAGE

        View view = inflater.inflate(R.layout.fragment_list_selfies, container, false);
        mRecyclerView = view.findViewById(R.id.selfies_recyclerview);

        //initialize lists
        final ArrayList<String> mPhotoIds = new ArrayList<>();
        final ArrayList<Bitmap> mPhotoArrayList = new ArrayList<>();

        //get names
        DatabaseReference usersPhotoRef = mDatabaseIns.getReference().child("storeIDs");

        usersPhotoRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                final long ONE_MEGABYTE = 1024 * 1024;
                for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {

                    String image_name = uniqueKeySnapshot.getValue(String.class);
                    Log.i("IMAGE_NAME", image_name);
                    mPhotoIds.add(image_name);
                    String photoId = image_name + ".png";
                    StorageReference photoRef = mStorageIns.getReference().child("images/" + photoId);
                    photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                        @Override
                        public void onSuccess(byte[] bytes) {
                            Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                            mPhotoArrayList.add(bitmap);
                            Log.i("GET_BITMAP", String.valueOf(mPhotoArrayList.size()));
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.i("SELFIEFRAGMENT", e.getMessage());
                        }
                    });
                    Log.i("SELFIES", String.valueOf(mPhotoArrayList.size()));
                }

                SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
                mRecyclerView.setAdapter(selfiesAdapter);

                mLayoutManager = new GridLayoutManager(getActivity(), 2);
                mRecyclerView.setLayoutManager(mLayoutManager);
                mRecyclerView.setItemAnimator(new DefaultItemAnimator());
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Log.i("SELFIEFRAGMENT", databaseError.getMessage());
            }
        });

        return view;
    }

}


0
user70 30 jun. 2019 a las 15:08

1 respuesta

La mejor respuesta

Cada vez que agrega una foto al mPhotoArrayList, debe informarle al adaptador que su conjunto de datos ha cambiado llamando notifyDataSetChanged() en él. El adaptador luego retará la vista.

usersPhotoRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        final long ONE_MEGABYTE = 1024 * 1024;
        for (DataSnapshot uniqueKeySnapshot : dataSnapshot.getChildren()) {

            String image_name = uniqueKeySnapshot.getValue(String.class);
            Log.i("IMAGE_NAME", image_name);
            mPhotoIds.add(image_name);
            String photoId = image_name + ".png";
            StorageReference photoRef = mStorageIns.getReference().child("images/" + photoId);
            photoRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                @Override
                public void onSuccess(byte[] bytes) {
                    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                    mPhotoArrayList.add(bitmap);
                    Log.i("GET_BITMAP", String.valueOf(mPhotoArrayList.size()));

                    selfiesAdapter.notifyDataSetChanged();
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.i("SELFIEFRAGMENT", e.getMessage());
                }
            });
            Log.i("SELFIES", String.valueOf(mPhotoArrayList.size()));
        }

Para permitir que la devolución de llamada encuentre el adaptador, deberá declararlo como final:

        final SelfiesAdapter selfiesAdapter = new SelfiesAdapter(getActivity(), mPhotoArrayList, mPhotoIds);
0
Frank van Puffelen 30 jun. 2019 a las 12:59