gpt4 book ai didi

java - 我被作业困住了,无法在 sqlite 数据库 android 中保存图像路径

转载 作者:行者123 更新时间:2023-12-02 04:39:17 24 4
gpt4 key购买 nike

我是 Android 新手,我需要家庭作业帮助,我需要制作带有问题的测验应用程序,有些问题需要有图像,我在互联网上找到了一些东西,但我不知道该做什么接下来,我想从可绘制对象中获取图像并将其保存在数据库中,当问题被调用时,图像出现在 imageview 中

    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);
}


}

最佳答案

最好的方法是将图像保存在文件夹中,并将该图像的路径保存在数据库中。当提出问题时,从数据库中获取该图像的位置并从指定的文件夹中加载。

关于java - 我被作业困住了,无法在 sqlite 数据库 android 中保存图像路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56528009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com