Firebase Storage: Problem with downloading the image

1

I have a problem when it comes to obtaining the image from the storage. I am currently using a recyclerview and cardview to bring the image. As you can see below I can bring the name of the image, but I can not bring the image.

FirebaseDataBase:

Adater code:

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context Context;
private List<Upload> mUploads;

public ImageAdapter(Context context, List<Upload> uploads) {
    Context = context;
    mUploads = uploads;
}

@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(Context).inflate(R.layout.image_item, parent, false);
    return new ImageViewHolder(v);
}

@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    holder.textViewName.setText(uploadCurrent.getName());
    Picasso.get()
            .load(uploadCurrent.getImageUrl())
            .fit()
            .centerCrop()
            .into(holder.imageView);
}

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

public class ImageViewHolder extends RecyclerView.ViewHolder {
    public TextView textViewName;
    public ImageView imageView;

    public ImageViewHolder(View itemView) {
        super(itemView);

        textViewName = itemView.findViewById(R.id.text_view_name);
        imageView = itemView.findViewById(R.id.image_view_upload);
    }
}
}

Main activity code

public class ViewImagesActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private ImageAdapter mAdapter;

private ProgressBar mProgressCircle;

private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_images);

    mRecyclerView = findViewById(R.id.RecyclerView);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mProgressCircle = findViewById(R.id.progress_circle);

    mUploads = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("Posts");

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                mUploads.add(upload);
            }

            mAdapter = new ImageAdapter(ViewImagesActivity.this, mUploads);

            mRecyclerView.setAdapter(mAdapter);
            mProgressCircle.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ViewImagesActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            mProgressCircle.setVisibility(View.INVISIBLE);
        }
    });
}
}

Upload class

public class Upload {
   private String mName;
   private String mImageUrl;

   public Upload() {
       //empty constructor needed
   }

   public Upload(String name, String imageUrl, String user_id) {
       if (name.trim().equals("")) {
           name = "No Name";
       }

       mName = name;
       mImageUrl = imageUrl;
   }

   public String getName() {
       return mName;
   }

   public void setName(String name) {
       mName = name;
   }

   public String getImageUrl() {
       return mImageUrl;
   }

   public void setImageUrl(String imageUrl) {
       mImageUrl = imageUrl;
   }
}
    
asked by Ibrahim 03.01.2019 в 18:56
source

2 answers

1

In the Upload class you have misplaced the names of the variables

 private String mName;
 private String mImageUrl;

should have the same name as they are stored in Firebase

 private String name;
 private String fotoUri;

and generate the seters and getters with those names.

Now, when you add to the arraylist the url you can try doing this

for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                String fotoUrl = upload.getFotoUri();
                String name = upload.getName();
                upload.setFotoUri(fotoUrl);
                upload.setName(name);
                mUploads.add(upload);
            }

Then in your onBindViewHolder()

 Picasso.get()
            .load(uploadCurrent.getFotoUri())
            .fit()
            .centerCrop()
            .into(holder.imageView);
    
answered by 05.01.2019 / 03:25
source
2

Your Upload class should use fotoUri instead of mImageUrl because it is called in firebase.

Change it to the next one.

public class Upload {
    private String name;
    private String fotoUri;

    public Upload() {
        //empty constructor needed
    }

    public Upload(String name, String imageUrl, String user_id) {
        if (name.trim().equals("")) {
            name = "No Name";
        }

        this.name = name;
        this.fotoUri= fotoUri;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImageUrl() {
        return fotoUri;
    }

    public void setImageUrl(String imageUrl) {
        fotoUri = imageUrl;
    }
}
    
answered by 04.01.2019 в 19:12