如何在GridView中显示Firebase存储映像

时间:2020-08-19 15:18:23

标签: android firebase firebase-storage

我是Android的新手,我正在学习gridview..i显示从可绘制图像到gridview的图像列表,但我不知道如何在gridview中显示Firebase存储中的图像

我看过很多教程,并且尝试了很多Stackoverflow答案,但是我无法得到所需的东西

我在Firebase Storage中创建了一个新文件夹,并在那里上传了6张图片,我想在gridview中显示所有6张图片

这是我如何显示来自drawable的图像的代码,我不知道如何加载该URL,在滑行过程中,我完全感到困惑和混乱

public class NewListCreate extends BottomSheetDialogFragment {


   int[] images = {R.drawable.menu, R.drawable.musicbox, R.drawable.shoppingbag, R.drawable.shoppingcart, R.drawable.wallet, R.drawable.weddingdress};
    int imageRes = images[0];

  

    public NewListCreate() {
    }

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

        View view = inflater.inflate(R.layout.new_list_create, container, false);

        ImageButton done = view.findViewById(R.id.done);
        final EditText listname = (EditText) view.findViewById(R.id.listname);
        final GridView gridView = (GridView) view.findViewById(R.id.gridview);

        final CustomAdpter customAdpter = new CustomAdpter(images, getContext());
        gridView.setAdapter(customAdpter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                customAdpter.selectedImage = i;
                customAdpter.notifyDataSetChanged();
                imageRes = images[i];


            }
        });




        done.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                String itemname = listname.getText().toString();
                if (!TextUtils.isEmpty(listname.getText().toString())) {

                    startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image", imageRes));
                    dismiss();
                } else {
                    Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
                }

            }

        });


        return view;


    }


    public class CustomAdpter extends BaseAdapter {

        public int selectedImage = 0;
        private int[] icons;
        private Context context;
        private LayoutInflater layoutInflater;

        public CustomAdpter(int[] icons, Context context) {
            this.icons = icons;
            this.context = context;
            this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return icons.length;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            if (view == null) {
                view = layoutInflater.inflate(R.layout.image_list, viewGroup, false);

            }
            StorageReference storageReference = FirebaseStorage.getInstance().getReference();
            ImageView imageicons = view.findViewById(R.id.image);

            if (i < icons.length) {

                imageicons.setImageResource(icons[i]);

                if (i != selectedImage) {
                    imageicons.setImageAlpha(50);
                }
                imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
                // imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
                if (i == selectedImage) {

                    view.setBackgroundColor(Color.WHITE);
                } else {
                    view.setBackgroundColor(Color.TRANSPARENT);
                }
            }
            ;


            return view;
        }
    }
}

这是Firebase的存储信息

enter image description here

3 个答案:

答案 0 :(得分:0)

我创建了一个 FirebaseUtils 类,从中我可以选择一系列静态 Firebase 方法(无双关语)。
为了实现你想要的,只需创建一个循环然后调用下面的downloadPic方法

<ul v-for="(item, key) in actividades" :key="key">

ivPic 是图像视图
picToDisplay是从存储下载的图片
storageFolder 是 firebase 上的文件夹,例如:Photos/
picToDownloadstorageFolder
中图片的名称 noOfPhotos 是您拥有的照片数量,显然您可以设置查询并将它们加载到 for( int index = 0; index < noOfPhotos; index++ ) FirebaseUtils.downloadPic( ivPic[index], "Photos/", image[index] ); 中以方便

ArrayList

答案 1 :(得分:-1)

我使用以下代码在我的应用中进行了小型快速演示,结果是enter image description here

对于gridAdappter,我有以下内容:

public class GridAdapter extends BaseAdapter {
Context context;
private final String[] values;
private final String[] images;
View view;
LayoutInflater layoutInflater;

public GridAdapter(Context context, String[] values, String[] images) {
    this.context = context;
    this.values = values;
    this.images = images;
}

@Override
public int getCount() {
    return values.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(convertView==null)
    {
        view = new View(context);
        view=layoutInflater.inflate(R.layout.single_item,null);
        ImageView imageView = view.findViewById(R.id.imageView);
        TextView textView = view.findViewById(R.id.textView);
        Glide.with(context).load(images[position]).into(imageView);
        textView.setText(values[position]);
    }
    return view;
}

}

对于我有GridView的活动,我做到了:

  gridView = findViewById(R.id.gridView) ;

   databaseReference = FirebaseDatabase.getInstance().getReference().child("Posts");
   databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
       @Override
       public void onDataChange(@NonNull DataSnapshot snapshot) {
           for(DataSnapshot dataSnapshot:snapshot.getChildren())
           {
               if(i<19)
               {
                   Post post = dataSnapshot.getValue(Post.class);
                   images[i]=post.getpImage();
                   values[i]=post.getpTitle();
                   i++;
               }
           }
           GridAdapter gridAdapter = new GridAdapter(getApplicationContext(),values,images);
           gridView.setAdapter(gridAdapter);
       }

       @Override
       public void onCancelled(@NonNull DatabaseError error) {

       }
   });

我有一个Firebase数据库参考,以获取我存储在Firebase Storage上的图片的URL,并且我使用类Post来获取信息,因为它对我来说更容易,更快。

您需要获取图像的URL并将其添加到字符串数组图像中,如果您尚未使用存储的图像URL来建立数据库,请查看this文档以了解如何从您的存储中获取网址。

在这里,您可以从Firebase存储中获取一张图片的URL

storageReference.child("icons/menu").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            images[0] = String.valueOf(uri);
            values[0] = "menu"
          //Here you can also add thiss
         /*GridAdapter gridAdapter = new 
                      GridAdapter(getApplicationContext(),values,images);
       gridView.setAdapter(gridAdapter);*/

        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {

        }
    });

答案 2 :(得分:-2)

public class CustomAdpter extends BaseAdapter {

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();    

private Context context;

public CustomAdapter(Context c) {
    context = c;
}

public int getCount() {
    return thumbId.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(context);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(thumbId [position]);




    return imageView;
}

// references to our images
public Integer[] thumbId = {

        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};

}