如何从Firebase存储中加载图像?

时间:2019-05-03 16:10:33

标签: java android

我正在使用“片段活动”,无法从Android Studio中的Firebase存储加载图像。谁能帮助我解决问题?

public class MenuUser1 extends Fragment {

    private RecyclerView mPeopleRV;
    private DatabaseReference mDatabase;
    private EditText search;
    private Button searchproduct;
    private FirebaseRecyclerAdapter<Upload, MenuUser1.ItemViewHolder> mPeopleRVAdapter;

    View v;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //returning our layout file
        //change R.layout.yourlayoutfilename for each of your fragments
        v= inflater.inflate(R.layout.fragment_menu_user_1, container, false);

        mDatabase = FirebaseDatabase.getInstance().getReference().child("product");
        mDatabase.keepSynced(true);

        search = (EditText)v.findViewById(R.id.editText);
        searchproduct = (Button)v.findViewById(R.id.button2);
        mPeopleRV = (RecyclerView) v.findViewById(R.id.myRecycleView);

        DatabaseReference productRef = FirebaseDatabase.getInstance().getReference().child("product") ;
        Query productQuery = productRef.orderByKey();

        mPeopleRV.hasFixedSize();
        mPeopleRV.setLayoutManager(new LinearLayoutManager(getActivity()));

        FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<Upload>().setQuery(productQuery, Upload.class).build();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        mPeopleRV.setLayoutManager(gridLayoutManager);
        mPeopleRVAdapter = new FirebaseRecyclerAdapter<Upload, MenuUser1.ItemViewHolder>(personsOptions) {
            @Override
            protected void onBindViewHolder(MenuUser1.ItemViewHolder holder, final int position, final Upload model) {
                holder.setTitle(model.getItemName());
                holder.setImage(model.getImageUrl());
                holder.mView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final String url = model.getImageUrl();
                        Intent intent = new Intent(getActivity().getApplicationContext(), ItemWebView.class);
                        intent.putExtra("imageUrl", url);
                        startActivity(intent);
                    }
                });
            }

            @Override
            public MenuUser1.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_row, parent, false);
                return new MenuUser1.ItemViewHolder(view);
            }
        };

        mPeopleRV.setAdapter(mPeopleRVAdapter);
        return v;
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //you can set the title for your toolbar here for different fragments different titles
        getActivity().setTitle("HOME");
    }

    @Override
    public void onStart() {
        super.onStart();
        mPeopleRVAdapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        mPeopleRVAdapter.stopListening();


    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder
    {
        View mView;
        Upload u;
        public ItemViewHolder(View itemView){
            super(itemView);
            mView = itemView;
        }
        public void setTitle(String title){
            TextView post_title = (TextView)mView.findViewById(R.id.post_title);
            post_title.setText(title);
        }
        public void setImage(String image){
            ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
            Picasso.get().load(image).into(post_image);
        }    
    }
}

我希望显示Firebase中的图像和图像名称,如果可能的话,我也希望该图像是可单击的。

0 个答案:

没有答案