lundi 29 février 2016

add image in sqlite database

Hi i have a problem when inserting an android image in my datatabase for a quizz application.The questions must appear with a specific image,i trie to do as someone explained me here but i have an error in my method where i add the questions.I inserted the image like this.

This my database

package com.example.toureamidou.piste;

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

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

/**
 * Created by TOURE Amidou on 24/02/2016.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Question";
    private static final String TABLE_QUEST = "quest";
    private static final String KEY_ID = "id";
    private static final String KEY_QUES = "question";
    private static final String KEY_ANSWER = "answer";
    private static final String KEY_OPTA= "opta";
    private static final String KEY_OPTB= "optb";
    private static final String KEY_OPTC= "optc";
    private static final String KEY_IMAGE = "image";
    private SQLiteDatabase dbase;
    public DatabaseHelper(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_IMAGE+"TEXT)";
        db.execSQL(sql);
        addQuestions();
    }





    private void addQuestions() {
        Questions q1=new Questions("FFFFF","Tata", "Titi", "Toto", "Tata");
        this.addQuestion(q1);
        Questions q2=new Questions("Quel est le plus grand pays au monde", "Suisse", "Italie", "Chine", "Chine");
        this.addQuestion(q2);
        Questions q3=new Questions("Comment s'appele le président francais","Obama", "Hollande","Gorbatchev", "Hollande" );
        this.addQuestion(q3);
        Questions q4=new Questions("zzzzzzzzzzzzzzzzzzzz", "ali", "dede", "home","dede");
        this.addQuestion(q4);
        Questions q5=new Questions(" est l'homme le plus riche au monde","Gates","Trump","Carlos Slim","Trump");
        this.addQuestion(q5);

    }






    private void addQuestion(Questions quest,byte[] image) {
        Log.d("addQuestions", quest.toString());
        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_IMAGE,image);
        dbase.insert(TABLE_QUEST, null, values);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
        onCreate(db);
    }
    public List<Questions> getAllQuestions() {
        List<Questions> quesList = new ArrayList<Questions>();
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        dbase=this.getReadableDatabase();
        Cursor cursor = dbase.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                Questions quest = new Questions();
                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 quesList;
    }
    public int updateQuestions (Questions questions){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("questions",questions.getQUESTION());
        contentValues.put("answer",questions.getANSWER());
        contentValues.put("opta", questions.getOPTA());
        contentValues.put("optb", questions.getOPTB());
        contentValues.put("optc", questions.getOPTC());
        int i = db.update(TABLE_QUEST, contentValues, KEY_ID + " = ?", new String[]{String.valueOf(questions.getID())});
        db.close();
        return i;

    }

}

and this is my quizz activity class


package com.example.toureamidou.piste;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.util.List;
public class Quiz extends AppCompatActivity {
    List<Questions> quesList;
    int qid=0;
    Questions currentQ;
    TextView txtQuestion;
    RadioButton rda, rdb, rdc;
    Button butNext;
    DatabaseHelper myDb;
    MediaPlayer wrongsound;
    MediaPlayer winsound;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        myDb = new DatabaseHelper(this);
        quesList = myDb.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);
        wrongsound = MediaPlayer.create(this,R.raw.mywrong);
        winsound = MediaPlayer.create(this,R.raw.mysound);
        setQuestionsView();

        Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.quizz);
        ByteArrayOutputStream boss = new ByteArrayOutputStream();
        b.compress(Bitmap.CompressFormat.PNG,100,boss);
        byte[] img = boss.toByteArray();


        butNext.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                RadioGroup grp = (RadioGroup) Quiz.this.findViewById(R.id.radioGroup1);
                RadioButton answer = (RadioButton) Quiz.this.findViewById(grp.getCheckedRadioButtonId());
                Log.d("your answer", Quiz.this.currentQ.getANSWER() + " " + answer.getText());
                if (Quiz.this.currentQ.getANSWER().equals(answer.getText())) {

                    winsound.start();
                } else {
                    wrongsound.start();
                }

                if (Quiz.this.qid < 5) {
                    Quiz.this.currentQ = (Questions) Quiz.this.quesList.get(Quiz.this.qid);
                    Quiz.this.setQuestionsView();
                } else {
                    Intent intent = new Intent(Quiz.this, Result.class);
                    Bundle b = new Bundle();

                    intent.putExtras(b);
                    Quiz.this.startActivity(intent);
                    Quiz.this.finish();
                }

            }
        });



    }



    private void setQuestionsView() {
        txtQuestion.setText(currentQ.getQUESTION());
        rda.setText(currentQ.getOPTA());
        rdb.setText(currentQ.getOPTB());
        rdc.setText(currentQ.getOPTC());
        qid++;                                


    }







}

Aucun commentaire:

Enregistrer un commentaire