从Firebase检索图像(实时数据库)

时间:2019-03-06 16:34:47

标签: java android android-fragments firebase-realtime-database

我正在尝试从Firebase实时数据库中检索图像,但是我的应用在打开图像时始终崩溃。

数据库结构:

[https://i.stack.imgur.com/iWOEK.png][1]

以下是我的Java代码:

public class main2 extends AppCompatActivity {
    TextView txt, url;
    Button btn;
    ImageView img;
    private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference reference = firebaseDatabase.getReference();
    private DatabaseReference childreference = reference.child("url");

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        url = findViewById(R.id.text);
        img = findViewById(R.id.image);

        txt = (TextView) findViewById(R.id.mytext);
        btn = (Button) findViewById(R.id.mybutton);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn.setVisibility(View.GONE);
                txt.setText("JOINED, Details will be send by notification.");


            }

        });


    }

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

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

            }
        });
    }
}

Logcat:

I / Process:正在发送信号。 PID:8763 SIG:9

1 个答案:

答案 0 :(得分:0)

确保已授予对数据库规则的读写权限 { "rules": { ".read": true, ".write": true } }有关更多信息,请参见this