如何使用Picasso或Glide库将图像字符串路径加载到ImageView中?

时间:2016-09-28 04:36:46

标签: android firebase-realtime-database android-imageview picasso android-glide

所以我有一堆我从Firebase检索的图像字符串路径。对于每个字符串,我想使用Picasso或Glide将该图像上传到ImageView。 但是无论我做什么,图像都不会加载。应用程序没有崩溃,只是ImageView是空白的。

以下是我从Firebase数据库查询的代码。

 //Firebase
        mRef = new Firebase("https://frontier-e802a.firebaseio.com/");
        Firebase cardReference = mRef.child("cards");
        cardReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //Toast.makeText(getBaseContext(),String.valueOf(dataSnapshot),Toast.LENGTH_SHORT).show();
                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                    CardModel fireBaseCard = dataSnapshot1.getValue(CardModel.class);
                    testCardModelArray.add(fireBaseCard);

                }
                /*for(CardModel cardModel : testCardModelArray)
                {
                    Toast.makeText(getBaseContext(),cardModel.getImages().get(cardModel.getImages().size()-1),Toast.LENGTH_SHORT).show();

                }*/
                setupCards();
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });
        adapter = new SwipeDeckAdapter(testCardModelArray, this);


  private void setupCards()
    {
        testData = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            testData.add(String.valueOf(i));
        }

        adapter = new SwipeDeckAdapter(testCardModelArray, this);
        if(cardStack != null)
        {
            cardStack.setAdapter(adapter);
        }
        cardStack.setCallback(new SwipeDeck.SwipeDeckCallback() {
            @Override
            public void cardSwipedLeft(long stableId) {
                Log.i("ActivityCenter", "card was swiped left, position in adapter: " + stableId);
            }

            @Override
            public void cardSwipedRight(long stableId) {
                Log.i("ActivityCenter", "card was swiped right, position in adapter: " + stableId);
                Toast.makeText(getBaseContext(),String.valueOf(stableId),Toast.LENGTH_SHORT).show();

            }

            @Override
            public boolean isDragEnabled(long itemId) {
                return dragCheckbox.isChecked();
            }
        });

        cardStack.setLeftImage(R.id.left_image);
        cardStack.setRightImage(R.id.right_image);

        Button btn = (Button) findViewById(R.id.button_left);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cardStack.swipeTopCardLeft(500);

            }
        });
        Button btn2 = (Button) findViewById(R.id.button_right);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cardStack.swipeTopCardRight(180);
            }
        });

        Button btn3 = (Button) findViewById(R.id.button_center);
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                testData.add("a sample string.");
//                adapter.notifyDataSetChanged();
                cardStack.unSwipeCard();
            }
        });
    }

这是我的适配器,我试图使用Glide / Picasso和 NOTHING WORKS 。它太令人沮丧了。

 public class SwipeDeckAdapter extends BaseAdapter
    {
        private List<CardModel> data;
        private Context context;

        public SwipeDeckAdapter(List<CardModel> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        //No idea how these getItem and getItemId are used.
        @Override
        public Object getItem(int position)
        {
            return data.get(position).getCardID();
        }
        //Not used at all
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View v = convertView;
            if (v == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                // normally use a viewholder
                v = inflater.inflate(R.layout.test_card2, parent, false);
            }
            //((TextView) v.findViewById(R.id.textView2)).setText(data.get(position));

            ImageView imageView = (ImageView) v.findViewById(R.id.offer_image);
            TextView textView = (TextView) v.findViewById(R.id.sample_text);
            if(!data.get(position).getImages().isEmpty())
            {
                Glide.with(context).load(data.get(position).getImages().get(data.get(position).getImages().size()-1)).centerCrop().into(imageView);
                String item = data.get(position).getCardName();
                textView.setText(item);
            }
            else
            {
                Picasso.with(context).load(R.drawable.food).fit().centerCrop().into(imageView);
                String item = data.get(position).getCardName();
                textView.setText(item);

            }
            v.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Log.i("Layer type: ", Integer.toString(v.getLayerType()));
                    Log.i("Hardware Accel type:", Integer.toString(View.LAYER_TYPE_HARDWARE));
                    /*Intent i = new Intent(v.getContext(), BlankActivity.class);
                    v.getContext().startActivity(i);*/
                }
            });
            return v;
        }
    }

希望你们能提供见解。而且我知道我成功地查询了这些卡,因为我能够遍历所有的字符串并为它们干杯。

2 个答案:

答案 0 :(得分:2)

用于将图像从文件加载到imageview(滑行)的代码段:

Glide.with(context)
    .load(new File(path)) 
    .into(imageView);

答案 1 :(得分:1)

getItem方法中,您必须返回 data.get(position),而不是 data.get(position).getCardID();