首页 > 解决方案 > Android Firebase 实时数据库:如何在实时数据库中加载图像并在 imageview 中检索它

问题描述

我正在制作一个使用 firebase 实时数据库的测验应用程序。我想使用来自 realtimedatbase 的图像。我不知道如何将图像存储在 firebase 实时数据库中。谁能帮我解决这个问题?que1 的示例我想使用 image1,que2-image2 等等.....实时数据库 - JSON 文件

enter code here

public class MainActivity extends AppCompatActivity {

    private TextView mScoreView;
    private TextView mQuestion,  mButtonChoice1, mButtonChoice2, mButtonChoice3, mButtonChoice4;
    private Button next,prev;
    private ImageView mimageView;
    private int mScore=0;
    private  int mQuestionNumber=0;

    private String mAnswer;

    private Firebase mQuestionRef;
    private Firebase mchoice1Ref;
    private Firebase mchoice2Ref;
    private Firebase mchoice3Ref;
    private Firebase mchoice4Ref;
    private Firebase mAnswerRef;
  //  private Firebase mImageRef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FirebaseDatabase.getInstance().setPersistenceEnabled(true);
        Firebase.setAndroidContext(this);

       mimageView=(ImageView)findViewById(R.id.image);

      //  Resources res = getResources(); /** from an Activity */
        //mimageView.setImageDrawable(res.getDrawable(R.drawable.a));



        mScoreView =(TextView)findViewById(R.id.score);
        mQuestion=(TextView)findViewById(R.id.question);

        mButtonChoice1=(TextView)findViewById(R.id.choice1);
        mButtonChoice2=(TextView)findViewById(R.id.choice2);
        mButtonChoice3=(TextView)findViewById(R.id.choice3);
        mButtonChoice4=(TextView)findViewById(R.id.choice4);
        next=(Button)findViewById(R.id.quit1);
        prev=(Button)findViewById(R.id.quit);

        updateQuestion();

     next.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {


         if(mQuestionNumber<99) {
             mQuestionNumber++;
          //  mQuestion =  mQuestionRef.getParent(mQuestionNumber);
             updateQuestion();
         }
         else
             {
                 next.setEnabled(false);
             }
    }
});
        prev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(mQuestionNumber>0)
                {
                    mQuestionNumber--;
                    //  mQuestion =  mQuestionRef.getParent(mQuestionNumber);
                    updateQuestion();
                }

                else{ prev.setEnabled(false);}

            }
        });

        mButtonChoice1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice1.getText().equals(mAnswer)){
                    mScore = mScore + 1;
                    updateScore(mScore);
                   // updateQuestion();
                    //This line of code is optiona
                    Toast.makeText(MainActivity .this, "correct", Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                  //  updateQuestion();
                }
            }
        });

        mButtonChoice2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice2.getText().equals(mAnswer)){
                    mScore = mScore + 1;
                    updateScore(mScore);
                  //  updateQuestion();
                    //This line of code is optiona
                        Toast.makeText( MainActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {
                       Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                  //  updateQuestion();
                }
            }
        });
        mButtonChoice3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice3.getText().equals(mAnswer)){
                    mScore = mScore + 1;
                    updateScore(mScore);
                   // updateQuestion();
                    //This line of code is optiona
                        Toast.makeText( MainActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {
                       Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                   // updateQuestion();
                }
            }
        });
        mButtonChoice4.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                //My logic for Button goes in here

                if (mButtonChoice4.getText().equals(mAnswer)){
                    mScore = mScore + 1;
                    updateScore(mScore);
                   // updateQuestion();
                    //This line of code is optiona
                      Toast.makeText( MainActivity.this, "correct", Toast.LENGTH_SHORT).show();

                }else {
                       Toast.makeText(MainActivity.this, "wrong", Toast.LENGTH_SHORT).show();
                   // updateQuestion();
                }
            }
        });



    }

private void updateScore(int score)
{
    mScoreView.setText("" +mScore);
}



    public void updateQuestion()                   
 {       mQuestionRef = new Firebase("URLHere/" + mQuestionNumber + "/question");
        mQuestionRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String question = dataSnapshot.getValue(String.class);
                mQuestion.setText(question);


            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });




        if ( mQuestionNumber==1)
        {
           // mimageView = new Firebase("gs://maths-quiz-cb006.appspot.com");
          String url ="URL HERE";
         // Glide.with
        }
       // else{next.setEnabled(false);}

    }
}


<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_quiz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
    android:background="#79b6ff"
android:paddingTop="16dp"
android:orientation="vertical"
tools:context=".MainActivity">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Score"
        android:textSize="16sp"
        android:layout_alignParentLeft="true"
        android:id="@+id/score_text"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/score"
        android:layout_alignParentRight="true"
        android:text="0"
        android:textSize="20sp"/>


<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="200dp"

    android:layout_below="@+id/question"/>


<TextView
    android:layout_below="@+id/score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Q"
    android:textSize="20sp"
    android:padding="8dp"
    android:textColor="#000000"
    android:layout_marginBottom="3dp"

</ScrollView>`

标签: androidfirebasefirebase-realtime-database

解决方案


推荐阅读