无法在Firebase存储中存储的RecyclerView中显示图像(仅)

时间:2016-12-27 08:07:09

标签: android firebase android-recyclerview firebase-storage firebaseui

我是使用Firebase存储并且在RecyclerView中显示图像时遇到问题的新手。我使用FirebaseRecyclerAdapter来显示我的数据。数据和图片网址存储在Firebase数据库中。问题是只有图像没有在RecyclerView中显示。

你可以提供给我的任何帮助,这将是很好的。感谢。

以下是我的Recycler活动代码:

public class DownloadActivity extends AppCompatActivity {

ScaleAnimation shrinkAnim;
private RecyclerView mRecyclerView;
private TextView tvNoImages;
private static final String TAG = "DownloadActivty";


//Getting reference to Firebase Database
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference childref;
private static StorageReference mStorageRef;

//private static final String userId = "53";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.downlad_activtiy_layout);
    childref=database.child("Photos").getRef();

    mStorageRef = FirebaseStorage.getInstance().getReference();
    //Initializing our Recyclerview
    mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
    tvNoImages = (TextView) findViewById(R.id.tv_no_image);
    //scale animation to shrink floating actionbar
    shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

    Log.i(TAG, "Set firebase adapter");
    FirebaseRecyclerAdapter<Image_model,ImageViewHolder> adapter = new FirebaseRecyclerAdapter<Image_model, ImageViewHolder>(
            Image_model.class, R.layout.image_item, ImageViewHolder.class,
            //referencing the node where we want the database to store the data from our Object
            database
    )
    {
        @Override
        protected void populateViewHolder(ImageViewHolder viewHolder, Image_model model, int position)
        {
            Log.i(TAG, "Populate Image");
            viewHolder.bindImage(model);
        }
    };

    mRecyclerView.setAdapter(adapter);
    mRecyclerView.setHasFixedSize(true);
    mRecyclerView.setLayoutManager (new LinearLayoutManager(this));

//ViewHolder for Firebase UI
public static class ImageViewHolder extends RecyclerView.ViewHolder{

    TextView tvUserName;
    TextView tvImageName;
    RatingBar ratingBar;
    ImageView ivImage;
    ImageView image;
    View viewHolder;
    Context mContext;

    public ImageViewHolder(View v) {
        super(v);
        viewHolder=v;
        mContext = v.getContext();
        }
    public void bindImage(Image_model model)
    {
        tvUserName=(TextView)viewHolder.findViewById(R.id.user_name);
        tvImageName = (TextView) viewHolder.findViewById(R.id.image_name);
        ratingBar = (RatingBar) viewHolder.findViewById(R.id.like);
        ivImage = (ImageView) viewHolder.findViewById(R.id.img);

        tvUserName.setText(model.getUserName());
        tvImageName.setText(model.getImageName());
        ratingBar.setRating(model.getImageLike());
        Picasso.with(mContext).load(model.getImage()).into(ivImage);
        //Glide.loadImage(model.getImage(),ivImage);

    }
}
}

这是我的模特课

public class Image_model {

private String UserName;
private String ImageName;
private String ImageUrl;
private int ImageLike;

public Image_model(){

}
public Image_model(String UserName,String ImageName,String ImageUrl,int ImageLike){
    this.UserName=UserName;
    this.ImageName = ImageName;
    this.ImageUrl = ImageUrl;
    this.ImageLike  = ImageLike;
}

public String getUserName() {
    return UserName;
}

public void setUserName(String UserName) {
    this.UserName = UserName;
}

public String getImageName() {
    return ImageName;
}

public void setImageName(String ImageName) {
    this.ImageName = ImageName;
}

public String getImage() {
    return ImageUrl;
}

public void setImage(String ImageUrl) {
    this.ImageUrl = ImageUrl;
}

public int getImageLike() {
    return ImageLike;
}

public void setImageLike(int ImageLike) {
    this.ImageLike = ImageLike;
}
}

1 个答案:

答案 0 :(得分:0)

我不知道你在bindImage方法中做了什么。但请记住,您正在从模型中获取imageURL,而不是图像本身。我建议您使用Glide库将图像从URL设置为和imageView。此外,请记住,在Firebase控制台中,您应该使用&#34;下载网址&#34;而不是您的存储参考。

之类的东西
  

https://firebasestorage.googleapis.com/v0/b/yourAwesomeDatabase ....

我在我的应用中使用Glide从Firebase设置我的图片,并且我使用此方法:

  /**
    * Load an image with default values and default placeholder.
    * @param url photo Url.
    * @param imageView target view to load the desired image.
    */
   public static void loadImage(String url, ImageView imageView) {
      Context context = imageView.getContext();
      ColorDrawable cd = new ColorDrawable(ContextCompat.getColor(context, R.color.app_grey));
      Glide.with(context)
         .load(url)
         .placeholder(cd)
         .crossFade()
         .centerCrop()
         .into(imageView);
   }