如何从Android Studio中的Firebase实时数据库的URL中检索图像

时间:2019-03-08 09:18:43

标签: android firebase firebase-realtime-database imageurl

我正在尝试提取名为“ imageUrl”的URL,以便从Firebase Realtime数据库的URL中检索图像,如下所示:

这是Firebase实时数据库中的数据库结构:

enter image description here

我有这个数据库:

{
  "Image": {
    "01": {
      "id": "1",
      "name": "img1",
      "imageUrl": "https://www.pexels.com/photo/beach-calm-clouds-coast-457881/"
    },
    "02": {
      "id": "2",
      "name": "img2",
      "imageUrl": "https://www.pexels.com/photo/sea-beach-holiday-vacation-42151/"
    }
  }
}

这是Android Studio中的活动:

    public class BigToePose extends AppCompatActivity {

    private Context context;
    private ImageView img;

    private FirebaseDatabase database;
    private DatabaseReference mReference;
    private DatabaseReference childReference;

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

        context = this;
        img = findViewById(R.id.imageView);

        database = FirebaseDatabase.getInstance();
        mReference = database.getReference();
        childReference = mReference.child("Image").child("01");
    }

    @Override
    protected void onStart() {
        super.onStart();
        childReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String url = dataSnapshot.child("imageUrl").getValue(String.class);
                Picasso.with(context)
                        .load(url)
                        .into(img);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

我试图将此代码作为活动从Android Studio中的另一个活动移至该活动,但该活动似乎未显示。我如何使它工作?

2 个答案:

答案 0 :(得分:0)

由于您尝试使Firebase引用位于已执行方法之外,因此可能根本无法执行。 尝试通过更改如下代码:-

   frm = new JFrame();

   frmLayout = new BorderLayout();
   frmLayout.layoutContainer(frm.getContentPane());


   mainPnl = new MainPanel();
   sP = new SecondPanel();
   tP = new ThirdPanel();

   getContentPane().add(mainPnl, BorderLayout.WEST);
   getContentPane().add(sP, BorderLayout.EAST);
   getContentPane().add(tP, BorderLayout.SOUTH);

答案 1 :(得分:0)

我有一个类似的问题,我在分享我的例子。看看它如何为我工作。

{"Questions": {
"1": {
  "question": "One MB is equal to",
  "option1": "1024KB",
  "option2": "1024Byte",
  "option3": "1000KB",
  "option4": "1024GB",
  "answer": "1024KB",
  "hint": "https://www.pexels.com/photo/123-let-s-go-imaginary-text-704767/"
},
"2": {
  "question": "What s used to make computer chips",
  "option1": "copper",
  "option2": "steel",
  "option3": "silicon",
  "option4": "iron",
  "answer": "silicon",
  "hint": "https://www.pexels.com/photo/grayscale-photography-of-railroad-crossing-signage-981294/"
}}}

看到这是我在Android Studio中的代码。但是我的图像存储在Firebase存储中,而您的图像以像素为单位。没什么。

public class Hint extends MainActivity {

private TextView url;
private ImageView img;
DatabaseReference childReference;

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

    url = findViewById(R.id.mURLText);
    img = findViewById(R.id.mHintImage);

    childReference = FirebaseDatabase.getInstance().getReference().child("Questions").child("1").child("Hint");
}

@Override
protected void onStart() {
    super.onStart();
    childReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            String message = snapshot.getValue(String.class);
            url.setText(message);
            Picasso.get()
                    .load(message)
                    .into(img);
        }

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

        }
    });
}}

因此,在您的情况下,您的代码应如下所示:

public class BigToePose extends AppCompatActivity {

private Context context;
private ImageView img;

private FirebaseDatabase database;
private DatabaseReference mReference;
private DatabaseReference childReference;
private Int url; // for a new TextView you'll have to create which I've mentioned below

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

    context = this;
    url = findViewById(R.Id.mURLText); // you will have to add a textView in your XML file and initiate it here, like I did here
    img = findViewById(R.id.imageView);

    database = FirebaseDatabase.getInstance();
    mReference = database.getReference();
    childReference = mReference.child("Image").child("01").child(imageUrl);
}

@Override
protected void onStart() {
    super.onStart();
    childReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String message = dataSnapshot.getValue(String.class);
            url.setText(message);
            Picasso.get()
                    .load(message)
                    .into(img);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
  }
}

希望对您有所帮助。