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

时间:2019-06-10 14:05:44

标签: java android sqlite

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


}

2 个答案:

答案 0 :(得分:0)

您应该在数据库中存储图像的链接,并使用PicassoGlide将其加载到ImageView中。

答案 1 :(得分:0)

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