andriod studio. Using sqlite to save images and display them on the screen

20 Views Asked by At

I am a high school student who is learning coding. In the Android Studio app, i need to use the SQLite app to save the image and display one image per screen. There are 4 categories in total and there are family, food, etc. Do I have to bring the image path and save it? Or how do I save it? It is so difficult because there is no related information. Next is the relevant coding.

private static final String DATABASE_NAME = "image_db";
private static final int DATABASE_VERSION = 1;
private static final String TABEL_NAME = "images";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_IMAGE_PATH = "image_path";

public ImageDBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String createTableQuery = "CREATE TABLE " + TABLE_NAME + "("
            + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + COLUMN_IMAGE_PATH + " TEXT"
            + ")";
    db.execSQL(createTableQuery);
}
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);
 }

 public void addImage(String imagePath) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(COLUMN_IMAGE_PATH, imagePath);
    db.insert(TABLE_NAME, null, values);
    db.close();
}

public String getImagePath(int id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, new String[]{COLUMN_ID, COLUMN_IMAGE_PATH},
            COLUMN_ID + "=?", new String[]{String.valueOf(id)}, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
    @SuppressLint("Range") String imagePath =         

cursor.getString(cursor.getColumnIndex(COLUMN_IMAGE_PATH));
    cursor.close();
    db.close();
    return imagePath;
    }
}

When the screen comes out like this picture and you press the bottom button, another picture should come out.

The code above will not run. What should I do? If the entire code is faulty or if you know more about SQLite, please answer. This is an application I'm making for graduation. As a first-time student using SQLite, I'm having a hard time. If the overall code is weird, can I ask you to fix it? please... :(

0

There are 0 best solutions below