我是android的新手,我需要做作业的帮助,我需要制作带有问题的测验应用程序,有些问题需要图像,我在互联网上找到了一些东西,但是卡住了,不知道该怎么办接下来,我想从可绘制图像中获取图像并将其保存在数据库中,当调用问题时图像出现在图像视图中
package com.autoskola;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import com.autoskola.QuizContract.*;
import java.util.ArrayList;
import java.util.List;
public class QuizDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "TeorijskiTestovi.db";
public static final int DATABSE_VERSION = 1;
private SQLiteDatabase db;
public QuizDbHelper( Context context) {
super(context, DATABASE_NAME, null,DATABSE_VERSION);
}
建立数据库
@Override
public void onCreate(SQLiteDatabase db) {
this.db = db;
//Pravljenje sqlite database
final String SQL_CREATE_QUESTIONS_TABLE = "CREATE TABLE " +
QuestionsTable.TABLE_NAME + " ( " +
QuestionsTable._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
QuestionsTable.COLUMN_QUESTION + " TEXT, " +
QuestionsTable.COLUMN_OPTION1 + " TEXT, " +
QuestionsTable.COLUMN_OPTION2 + " TEXT, " +
QuestionsTable.COLUMN_OPTION3 + " TEXT, " +
QuestionsTable.COLUMN_ANSWER_NR + " INTEGER, " +
QuestionsTable.COLUMN_IMAGE + " BLOB NOT NULL" +
")";
db.execSQL(SQL_CREATE_QUESTIONS_TABLE);
fillQuestionsTable();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// za dalje azuriranje verzije i promene u db, izbrisace postojucu tabelu i napraviti novu
db.execSQL("DROP TABLE IF EXISTS " + QuestionsTable.TABLE_NAME );
onCreate(db);
}
提出问题,然后我放了一些随机图片,我将图片位置放在字符串中,这样就不会全是红色
// metoda za pravljenje pitanja
private void fillQuestionsTable() {
Question q1 = new Question("A is correct", "A", "B", "C", 1,"R.drawable.bmw");
addQuestion(q1);
Question q2 = new Question("B is correct", "A", "B", "C", 2,"R.drawable.bmw");
addQuestion(q2);
Question q3 = new Question("C is correct", "A", "B", "C", 3,"R.drawable.bmw");
addQuestion(q3);
Question q4 = new Question("A is correct again", "A", "B", "C", 1, "R.drawable.bmw");
addQuestion(q4);
Question q5 = new Question("B is correct again", "A", "B", "C", 2,"R.drawable.bmw");
addQuestion(q5);
}
// metoda za dodavanje pitanja u db
private void addQuestion(Question question) {
ContentValues cv = new ContentValues();
cv.put(QuestionsTable.COLUMN_QUESTION, question.getQuestion());
cv.put(QuestionsTable.COLUMN_OPTION1, question.getOption1());
cv.put(QuestionsTable.COLUMN_OPTION2, question.getOption2());
cv.put(QuestionsTable.COLUMN_OPTION3, question.getOption3());
cv.put(QuestionsTable.COLUMN_ANSWER_NR, question.getAnswerNr());
cv.put(QuestionsTable.COLUMN_IMAGE, question.getImageQ());
db.insert(QuestionsTable.TABLE_NAME, null, cv);
}
获取问题数据
// retrive data
public List<Question> getAllQuestions() {
List<Question> questionList = new ArrayList<>();
db = getReadableDatabase();
Cursor c = db.rawQuery("SELECT * FROM " + QuestionsTable.TABLE_NAME, null);
//
if(c.moveToFirst()) {
do {
Question question = new Question();
question.setQuestion(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_QUESTION)));
question.setOption1(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION1)));
question.setOption2(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION2)));
question.setOption3(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_OPTION3)));
question.setAnswerNr(c.getInt(c.getColumnIndex(QuestionsTable.COLUMN_ANSWER_NR)));
question.setImageQ(c.getString(c.getColumnIndex(QuestionsTable.COLUMN_IMAGE)));
questionList.add(question);
}while(c.moveToNext());
}
c.close();
return questionList;
}
}
我的DbBitmapUtility类
package com.autoskola;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import java.io.ByteArrayOutputStream;
public class DbBitmapUtility {
// konvertovanje bitmap u byte array
public static byte[] getBytes(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 0, stream);
return stream.toByteArray();
}
// konvertovanje byte array u bitmap
public static Bitmap getImage(byte[] image) {
return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}