1

I am making a quiz app which uses the firebase realtime database. I want to use the images in question from realtimedatbase. I don't know how to store image in firebase realtime database. Can anyone help me to fix the issue? Example for que1 i want to use image1, for que2-image2, and so on..... Realtime Database- JSON file

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>`
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • You should not store images in firebase database or any kind of database. It's better to use Firebase storage for this and store the reference or link to the images in Firebase Realtime Database. – 0xC0DED00D Jun 05 '18 at 21:42
  • Also, you should not put your project ids and other confidential info in the question details. Do not copy paste your complete code here. Try to find which is the problem area. Have a look at this - https://stackoverflow.com/help/mcve – 0xC0DED00D Jun 05 '18 at 21:46
  • Possible duplicate of [How to store and view images on firebase?](https://stackoverflow.com/questions/13955813/how-to-store-and-view-images-on-firebase) – 0xC0DED00D Jun 05 '18 at 21:46
  • I had upload image in firebase storage and copy URL in imageview but it not worked. Can you provide the code for this. – Ashutosh Singh Jun 05 '18 at 22:23
  • You can't copy URL in imageView. Try using Fresco or Picasso libraries, or have a look at this question - https://stackoverflow.com/questions/5776851/load-image-from-url – 0xC0DED00D Jun 06 '18 at 05:32
  • I am load the image using glide library from firebase storage but I have a problem if I use a condition that if ( mQuestionNumber==1) { String url ="URL HERE"; // Glide.with } them this will load image in all question. I want to load it in question 1 only . Can you help me for this in above my code? – Ashutosh Singh Jun 07 '18 at 20:04

0 Answers0