如何在Android Studio的问题数据库中添加图像?

时间:2016-08-27 07:06:57

标签: java android

我在Android Studio中为Android构建我的测验应用程序。我准备好了问题,但现在我需要在数据库中的相关问题中添加某些图像。例如,图像仅用于问题1,问题2的另一图像等等。

这是FlagDatabase.java代码:

package com.example.darel.geogame;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;


public class FlagDatabase extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "geo_flag";
    // tasks table name
    private static final String TABLE_QUEST = "quest";
    // tasks Table Columns names
    private static final String KEY_ID = "qid";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer"; // correct option
    private static final String KEY_OPTA = "opta"; // option A
    private static final String KEY_OPTB = "optb"; // option B
    private static final String KEY_OPTC = "optc"; // option C
    private static final String KEY_OPTD = "optd"; // option D

    private int currImage = 0;

    private SQLiteDatabase dbase;
    public FlagDatabase(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    @Override

    public void onCreate(SQLiteDatabase db) {
        dbase = db;
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " ( "
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES
                + " TEXT, " + KEY_ANSWER + " TEXT, " + KEY_OPTA + " TEXT, "
                + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, " + KEY_OPTD + " TEXT)";
        db.execSQL(sql);
        addQuestion();
// db.close();
    }
    private void addQuestion() {

        Question q1 = new Question("How many colours are there on the flag of Pahang?", "3", "2", "4", "1", "2");
        this.addQuestion(q1);
        Question q2 = new Question("The previous flag of Sarawak looked remarkably similar and inverted to the current flag of which European country?", "Czech Republic", "Austria", "Hungary", "Slovenia", "Czech Republic");
        this.addQuestion(q2);
        Question q3 = new Question("Which of the following state flags does NOT have the colour white?", "Selangor", "Perlis", "Johor", "Melaka", "Perlis");
        this.addQuestion(q3);
        Question q4 = new Question("What feature makes the flag of Sabah unique from the other states' flags?", "14-pointed star", "A sun with 9 rays", "The word SABAH", "Mountain", "Mountain");
        this.addQuestion(q4);
        Question q5 = new Question("Which of the following state flags has the colour blue?", "Penang", "Perak", "Kedah", "Sarawak", "Penang");
        this.addQuestion(q5);
        Question q6 = new Question("The new flag of the Federal Territories of Malaysia was adopted in which year?", "1999", "2003", "2006", "2007", "2006");
        this.addQuestion(q6);
        Question q7 = new Question("The flag of Negeri Sembilan has three colours. Which of the following colours is NOT included?", "Red", "Yellow", "White", "Black", "White");
        this.addQuestion(q7);
        Question q8 = new Question("The flag of Penang was adopted in which year?", "1936", "1957", "1943", "1949", "1949");
        this.addQuestion(q8);
        Question q9 = new Question("Kelantan has one of the most unique flags in Malaysia. What object is NOT featured in the flag?", "Keris", "Spears", "Star", "Shield", "Shield");
        this.addQuestion(q9);
        Question q10 = new Question("What does the yellow colour symbolize in the flag of Sarawak?", "Harmony", "Southeast Asian royalty", "Eastern culture", "The Sun's rays", "Southeast Asian royalty");
        this.addQuestion(q10);
        Question q11 = new Question("Arrange the colours seen on the flag of Perak from top to bottom.", "White, Black, Yellow", "Yellow, White, Black", "Black, Yellow, White", "White, Yellow, Black", "White, Yellow, Black");
        this.addQuestion(q11);
        Question q12 = new Question("Which of the following state flags does NOT have the crescent and star?", "Terengganu", "Melaka", "Negeri Sembilan", "Johor", "Negeri Sembilan");
        this.addQuestion(q12);
        Question q13 = new Question("Which state's flag was claimed to be too simple and dull in 2006?", "Terengganu", "Pahang", "Perlis", "Kedah", "Terengganu");
        this.addQuestion(q13);
        Question q14 = new Question("How many shades of blue are seen on the flag of Sabah?", "3", "4", "1", "2", "3");
        this.addQuestion(q14);
        Question q15 = new Question("What year was the current flag of Sabah adopted?", "1981", "1983", "1986", "1988", "1988");
        this.addQuestion(q15);
        Question q16 = new Question("How many colours are featured on the flag of Malacca?", "2", "4", "3", "5", "4");
        this.addQuestion(q16);
        Question q17 = new Question("Selangor's first flag in the 1780s looked remarkably similar to the flag of which European country?", "Spain", "Germany", "Netherlands", "Ukraine", "Spain");
        this.addQuestion(q17);
        Question q18 = new Question("If you invert the flag of Ukraine, which state would resemble that flag?", "Perak", "Perlis", "Pahang", "Federal Territory", "Perlis");
        this.addQuestion(q18);
        Question q19 = new Question("The dominant colour of Kedah is red. What does the red symbolize?", "Wealth", "Blood shed", "Prosperity", "Agility", "Prosperity");
        this.addQuestion(q19);
        Question q20 = new Question("Which of the following state flags is considered tri-coloured?", "Perlis", "Perak", "Pahang", "Terengganu", "Perak");
        this.addQuestion(q20);
        Question q21 = new Question("What type of blue is used for the Mount Kinabalu silhouette in the flag of Sabah?", "Royal", "Navy", "Zircon", "Icicle", "Royal");
        this.addQuestion(q21);
// END
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
// Create tables again
        onCreate(db);
    }
    // Adding new question
    public void addQuestion(Question quest) {
// SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_QUES, quest.getQUESTION());
        values.put(KEY_ANSWER, quest.getANSWER());
        values.put(KEY_OPTA, quest.getOPTA());
        values.put(KEY_OPTB, quest.getOPTB());
        values.put(KEY_OPTC, quest.getOPTC());
        values.put(KEY_OPTD, quest.getOPTD());
// Inserting Row
        dbase.insert(TABLE_QUEST, null, values);
    }
    public List<Question> getAllQuestions() {
        List<Question> quesList = new ArrayList<Question>();
// Select All Query
        String selectQuery = "SELECT * FROM " + TABLE_QUEST + " ORDER BY RANDOM()";
        dbase = this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
// looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Question quest = new Question();
                quest.setID(cursor.getInt(0));
                quest.setQUESTION(cursor.getString(1));
                quest.setANSWER(cursor.getString(2));
                quest.setOPTA(cursor.getString(3));
                quest.setOPTB(cursor.getString(4));
                quest.setOPTC(cursor.getString(5));
                quest.setOPTD(cursor.getString(6));
                quesList.add(quest);
            } while (cursor.moveToNext());
        }
// return quest list
        return quesList;
    }
}

这是Question.java文件:

package com.example.darel.geogame;

import android.app.Activity;

public class Question extends Activity {
    private int ID;
    private String QUESTION;
    private String OPTA;
    private String OPTB;
    private String OPTC;
    private String OPTD;
    private String ANSWER;
    public Question() {
        ID = 0;
        QUESTION = "";
        OPTA = "";
        OPTB = "";
        OPTC = "";
        OPTD = "";
        ANSWER = "";
    }
    public Question(String qUESTION, String oPTA, String oPTB, String oPTC, String oPTD,
                    String aNSWER) {
        QUESTION = qUESTION;
        OPTA = oPTA;
        OPTB = oPTB;
        OPTC = oPTC;
        OPTD = oPTD;
        ANSWER = aNSWER;
    }
    public int getID() {
        return ID;
    }
    public String getQUESTION() {
        return QUESTION;
    }
    public String getOPTA() {
        return OPTA;
    }
    public String getOPTB() {
        return OPTB;
    }
    public String getOPTC() {
        return OPTC;
    }
    public String getOPTD() {
        return OPTD;
    }
    public String getANSWER() {
        return ANSWER;
    }
    public void setID(int id) {
        ID = id;
    }
    public void setQUESTION(String qUESTION) {
        QUESTION = qUESTION;
    }
    public void setOPTA(String oPTA) {
        OPTA = oPTA;
    }
    public void setOPTB(String oPTB) {
        OPTB = oPTB;
    }
    public void setOPTC(String oPTC) {
        OPTC = oPTC;
    }
    public void setOPTD(String oPTD) {
        OPTD = oPTD;
    }
    public void setANSWER(String aNSWER) {
        ANSWER = aNSWER;
    }
}

QuestionActivity.java

package com.example.darel.geogame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.List;


public class QuestionActivity extends Activity {
    List<Question> quesList;
    int score = 0;
    int qid = 0;
    Question currentQ;
    TextView txtQuestion, times, scored;
    Button button1, button2, button3, button4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FlagDatabase db = new FlagDatabase(this); // my question bank class
        quesList = db.getAllQuestions(); // this will fetch all quetonall questions
        currentQ = quesList.get(qid); // the current question
        txtQuestion = (TextView) findViewById(R.id.txtQuestion);
        times = (TextView) findViewById(R.id.timers);
        times.setText("");
// the textview in which the question will be displayed
// the three buttons,
// the idea is to set the text of three buttons with the options from question bank
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
// the textview in which score will be displayed
        scored = (TextView) findViewById(R.id.score);

// method which will set the things up for our game
        setQuestionView();
// button click listeners
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// passing the button text to other method
// to check whether the anser is correct or not
// same for all three buttons
                getAnswer(button1.getText().toString());
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAnswer(button2.getText().toString());
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAnswer(button3.getText().toString());
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAnswer(button4.getText().toString());
            }
        });
    }
    public void getAnswer(String AnswerString) {
        if (currentQ.getANSWER().equals(AnswerString)) {
// if conditions matches increase the int (score) by 1
// and set the text of the score view
            score++;
            scored.setText("Score : " + score);
        }
        if (qid < 10) {
// if questions are not over then do this
            currentQ = quesList.get(qid);
            setQuestionView();
        }
        else {
// if over do this
            Intent intent = new Intent(QuestionActivity.this,
                    ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", score); // Your score
            intent.putExtras(b); // Put your score to your next
            startActivity(intent);
            finish();
        }
    }

    private void setQuestionView() {
// the method which will put all things together
        txtQuestion.setText(currentQ.getQUESTION());
        button1.setText(currentQ.getOPTA());
        button2.setText(currentQ.getOPTB());
        button3.setText(currentQ.getOPTC());
        button4.setText(currentQ.getOPTD());
        qid++;
    }
}

这是设计界面的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/relatively"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d15400"
    android:orientation="vertical"
    android:weightSum="1" >
    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.90"
        android:orientation="vertical"
        android:weightSum="1"
        android:background="@drawable/interface12">
        <LinearLayout
            android:id="@+id/linearLayout11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.20"
            android:orientation="vertical"
            android:weightSum="1" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="horizontal" >
                <TextView
                    android:id="@+id/score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="Score : 0"
                    android:textColor="#000000"
                    android:textSize="25.0sp"
                    android:textStyle="bold" />
                <TextView
                    android:id="@+id/timers"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginRight="10dp"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="00:00:49"
                    android:textColor="#000000"
                    android:textSize="25.0sp"
                    android:textStyle="bold" />
            </LinearLayout>

        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">



        <LinearLayout
            android:id="@+id/linearLayout12"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.60"
            android:orientation="vertical"
            android:weightSum="1" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.40" >
                <TextView
                    android:id="@+id/txtQuestion"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginTop="5dp"
                    android:gravity="center"
                    android:text="15*2*1-1"
                    android:textColor="#000000"
                    android:textSize="20.0sp"
                    android:textStyle="bold" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="0.20"
                android:orientation="vertical"
                android:weightSum="1" >
                <Button
                    android:id="@+id/button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff821"
                    android:gravity="center"
                    android:text="30"
                    android:textColor="#000000"
                    android:textSize="20.0sp" />
                <Button
                    android:id="@+id/button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff821"
                    android:gravity="center"
                    android:text="29"
                    android:textColor="#000000"
                    android:textSize="20.0sp" />
                <Button
                    android:id="@+id/button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff821"
                    android:gravity="center"
                    android:text="32"
                    android:textColor="#000000"
                    android:textSize="20.0sp" />
                <Button
                    android:id="@+id/button4"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="80dp"
                    android:layout_marginRight="80dp"
                    android:layout_marginTop="5dp"
                    android:background="#fff821"
                    android:gravity="center"
                    android:text="32"
                    android:textColor="#000000"
                    android:textSize="20.0sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
    </LinearLayout>

2 个答案:

答案 0 :(得分:0)

您必须使用“blob”来存储图像。

  byte[] data = getBitmapAsByteArray(img);

答案 1 :(得分:-1)

您可以在数据库中添加名为&#39; img_link&#39;例如。此列将包含图像的URL(如果它是在线图像)或可绘制文件名(如果图像包含在您的Android应用程序中),则可以简单地加载图像的内容(使用与您的每个测验问题相关联的ImageView中的该列的值。