0

How can I set picture as question and retrieve it from my sd card in emulator?

I have a picture stored in sd card and I want to set that picture as a question. Should I provide IageView? Here's the code:

DbHelper.java

package dota.quiz;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    // Database Name
    private static final String DATABASE_NAME = "Dota";
    // tasks table name
    private static final String TABLE_QUEST = "quiz";
    // tasks Table Columns names
    private static final String KEY_ID = "id";
    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 SQLiteDatabase dbase;

    public DbHelper(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)";
        db.execSQL(sql);
        addQuestions();
        // db.close();
    }

    private void addQuestions() {
        Question q1 = new Question("EAGLESONG + TALISMAN OF EVASION + QUARTERSTAFF = ?", "DAGON", "VANGUARD", "BUTTERFLY", "BUTTERFLY");
        this.addQuestion(q1);
        Question q2 = new Question("SANGE + YASHA = ?", "EYE OF SKADI", "SHIVAS GUARD", "SANGE AND     YASHA", "SANGE AND YASHA");
        this.addQuestion(q2);
        Question q3 = new Question("ENERGY BOOSTER + VITALITY BOOSTER + POINT BOOSTER = ?", "REFRESHER ORB", "AGHANIM'S SCEPTER", "BLOODSTONE", "BLOODSTONE");
        this.addQuestion(q3);
        Question q4 = new Question("BOOTS OF SPEED + BLADES OF ATTACK + BLADES OF ATTACK = ?", "POWER     TREADS", "PHASE BOOTS", "ARCANE BOOTS", "PHASE BOOTS");
        this.addQuestion(q4);
        Question q5 = new Question("DEMON EDGE + SACRED RELIC = ?", "RADIANCE", "DAEDALUS", "DIVINE     RAPIER", "DIVINE RAPIER");
        this.addQuestion(q5);
    }

    @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());
        // 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;
        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));
                quesList.add(quest);
            } while (cursor.moveToNext());
        }
        // return quest list
        return quesList;
    }

    public int rowcount() {
        int row = 0;
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        row = cursor.getCount();
        return row;
    }
}

Question.java

package dota.quiz;

public class Question {
    private int ID;
    private String QUESTION;
    private String OPTA;
    private String OPTB;
    private String OPTC;
    private String ANSWER;

    public Question() {
        ID = 0;
        QUESTION = "";
        OPTA = "";
        OPTB = "";
        OPTC = "";
        ANSWER = "";
    }

    public Question(String qUESTION, String oPTA, String oPTB, String oPTC, String aNSWER) {
        QUESTION = qUESTION;
        OPTA = oPTA;
        OPTB = oPTB;
        OPTC = oPTC;
        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 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 setANSWER(String aNSWER) {
        ANSWER = aNSWER;
    }
}

QuizActivity.java

package dota.quiz;
import java.util.List;

import dota.quiz.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class QuizActivity extends Activity {
List<Question> quesList;
int score=0;
int qid=0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc;
Button butNext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz);
DbHelper db=new DbHelper(this);
quesList=db.getAllQuestions();
currentQ=quesList.get(qid);
txtQuestion=(TextView)findViewById(R.id.textView1);
rda=(RadioButton)findViewById(R.id.radio0);
rdb=(RadioButton)findViewById(R.id.radio1);
rdc=(RadioButton)findViewById(R.id.radio2);
butNext=(Button)findViewById(R.id.button1);
setQuestionView();
butNext.setOnClickListener(new View.OnClickListener() {     
@Override
public void onClick(View v) {
RadioGroup grp=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton answer=(RadioButton)findViewById(grp.getCheckedRadioButtonId());
Log.d("yourans", currentQ.getANSWER()+" "+answer.getText());
if(currentQ.getANSWER().equals(answer.getText()))
{
score++;
Log.d("score", "Your score"+score);
}
if(qid<5){                  
currentQ=quesList.get(qid);
setQuestionView();
}else{
intent = new Intent(QuizActivity.this, ResultActivity.class);
undle b = new Bundle();
b.putInt("score", score); //Your score
intent.putExtras(b); //Put your score to your next Intent
startActivity(intent);
finish();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_quiz, menu);
return true;
}
private void setQuestionView()
{
txtQuestion.setText(currentQ.getQUESTION());
rda.setText(currentQ.getOPTA());
rdb.setText(currentQ.getOPTB());
rdc.setText(currentQ.getOPTC());
qid++;
}
}

activity_quiz.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.04" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RadioButton" />
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/str_next" />

</LinearLayout>
Community
  • 1
  • 1
Haj
  • 13
  • 5

1 Answers1

0

Do you want to store the image files in the internal SQL database of the app? Then use a field of BLOB type, as explained here.

Do you want to store the image files as regular files on the SD card? Then use ordinary load/save file functions as explained here.

Community
  • 1
  • 1
thelawnmowerman
  • 11,956
  • 2
  • 23
  • 36
  • ur R answering or asking another question – Shakeeb Ayaz Jan 30 '14 at 08:47
  • I am answering the question with two possible alternatives. – thelawnmowerman Jan 30 '14 at 08:48
  • can u explain.. wat is question – Shakeeb Ayaz Jan 30 '14 at 08:49
  • BTW, StackOverflow is full of this kind of answers with several alternatives; in fact, this is usually the best type of answer. No offense, maybe you don't read/write/understand English properly and that's the source of the problem. – thelawnmowerman Jan 30 '14 at 08:53
  • Here. I want to add picture in my activity_quiz.xml. I want to set that picture as question. This app is soemthing a trivia quiz but it has a picture. not only word. – Haj Jan 30 '14 at 09:27
  • @Haj, did you solve your problem? Do you want to store your pictures (questions) as files or inside de internal SQLite database? – thelawnmowerman Jan 30 '14 at 16:41
  • @thelawnmowerman I can't solve my problem. No. I have already pictures (questions) stored in my sd card. But, I want that pictures put it into xml together with the 3 radio buttons. For example, I have game.xml contain 1 picture and 3 radio buttons. One of the radio button has a correct answer of what picture is that. For instance, the picture is an android logo, so I will put 2 incorrect and 1 correct answer like this "Android Logo, Apple, Samsung" – Haj Feb 03 '14 at 03:08
  • @ShakeebAyaz I am asking a question. – Haj Feb 03 '14 at 04:06
  • @ShakeebAyaz I already solve my problem. And now how can I set another picture when I click the next button? For example, in 1st question the picture is android logo then 3 radio buttons. 1 of the radio buttons are correct answer lets say rb1= Android Logo, rb2= Apple, rb3= Samsung then the correct is rb1. In the 2nd question is another picture again. Then, how can I set that picture when I click the next button to the next question? – Haj Feb 03 '14 at 08:52
  • @Haj, great, then you should mark this answer as accepted, if it was helpful. If you have a new question (no matters if it is related), you should start a new thread in StackOverflow). – thelawnmowerman Feb 03 '14 at 09:41