如何使用Android存储图像并从SQLite中检索图像?

时间:2022-09-03 23:14:13

I've tried to follow other answers for similar question and watched videos but got no results that really helps me. How do I get write a code that helps me store an image in SQLite and retrieving it?

我试图按照类似问题的其他答案和观看视频,但没有得到真正帮助我的结果。如何编写代码来帮助我在SQLite中存储图像并检索它?

So this is my code for inserting it into the database (Database.java)

所以这是我将其插入数据库的代码(Database.java)

public boolean insertFoodcourt(int id, String desc, String path){
        SQLiteDatabase db = this.getWritableDatabase();

        try {
            FileInputStream fs = new FileInputStream(path);
            byte[] imgbyte = new byte[fs.available()];
            fs.read(imgbyte);

            ContentValues contentvalues = new ContentValues();
            contentvalues.put("foodcourtid", id);
            contentvalues.put("foodcourtdesc", desc);
            contentvalues.put("foodcourtimg", imgbyte);

            db.insert("Foodcourt", null, contentvalues);
            fs.close();

            return true;

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

    }

Here's is me manually inserting the data into the database (MainPage.java)

这是我手动将数据插入数据库(MainPage.java)

db.insertFoodcourt(1, "Foodcourt 1", "@drawable/fc1");

And here is me trying to retrieve the image from the database (Database.java)

这是我试图从数据库中检索图像(Database.java)

public Bitmap getFoodcourtImg (int id){
    SQLiteDatabase db = this.getReadableDatabase();
    Bitmap fcImg = null;
    Cursor cursor = db.rawQuery("SELECT * FROM Foodcourt WHERE foodcourtid = ?", new String[]{String.valueOf(id)});
    if(cursor.moveToNext()){
        byte[] img = cursor.getBlob(cursor.getColumnIndex("foodcourtimg"));
        fcImg = BitmapFactory.decodeByteArray(img, 0, img.length);
    }
    return fcImg;
}

The only problem is retrieving the image and trying to put it into an ImageView (of a CardView). Before I edited the path/"foodcourtimg", I used this code and was able to get the description so I know that the code works, it's only the image part where I have a problem with.

唯一的问题是检索图像并尝试将其放入ImageView(CardView)中。在我编辑路径/“foodcourtimg”之前,我使用了这段代码并且能够获得描述,因此我知道代码可以工作,它只是我遇到问题的图像部分。

public String getFoodcourtDesc (int id){
    SQLiteDatabase db = this.getReadableDatabase();
    String fcDesc = null;
    Cursor cursor = db.rawQuery("SELECT * FROM Foodcourt WHERE foodcourtid = ?", new String[]{String.valueOf(id)});
    if(cursor.moveToNext()){
        fcDesc = cursor.getString(cursor.getColumnIndex("foodcourtdesc"));
    }
    return fcDesc;
}

Help would be greatly appreciated. If TL;DR, the first 3 codes are in need of help. The method to insert an image into the database, me inserting the image (or the path in this case) and the method to pull out the image. Thanks!

非常感谢帮助。如果TL; DR,前3个代码需要帮助。将图像插入数据库的方法,插入图像(或本例中的路径)以及拉出图像的方法。谢谢!

2 个解决方案

#1


2  

Why are you trying to save a drawable in a database that is already stored in your app?
You can just save its name as a string: "fc1" and retrieve it as a string, say in a variable name.
After that by:

为什么要尝试将drawable保存在已存储在应用程序中的数据库中?您可以将其名称保存为字符串:“fc1”并将其作为字符串检索,例如以变量名称。之后:

int id = resources.getIdentifier(name, "drawable", context.getPackageName());

you get its id and you get the drawable:

你得到它的身份,你得到了可绘制的:

Drawable d = getResources().getDrawable(id);

and you can use it as you like.

你可以随意使用它。

#2


0  

I have one solution instead of save byte[] in db try to store image in internal memory of your application directory Check here and then save the image path in your table.

我有一个解决方案,而不是在db中保存byte []尝试将图像存储在应用程序目录的内部存储器中检查此处,然后将图像路径保存在表中。

#1


2  

Why are you trying to save a drawable in a database that is already stored in your app?
You can just save its name as a string: "fc1" and retrieve it as a string, say in a variable name.
After that by:

为什么要尝试将drawable保存在已存储在应用程序中的数据库中?您可以将其名称保存为字符串:“fc1”并将其作为字符串检索,例如以变量名称。之后:

int id = resources.getIdentifier(name, "drawable", context.getPackageName());

you get its id and you get the drawable:

你得到它的身份,你得到了可绘制的:

Drawable d = getResources().getDrawable(id);

and you can use it as you like.

你可以随意使用它。

#2


0  

I have one solution instead of save byte[] in db try to store image in internal memory of your application directory Check here and then save the image path in your table.

我有一个解决方案,而不是在db中保存byte []尝试将图像存储在应用程序目录的内部存储器中检查此处,然后将图像路径保存在表中。