我想将我的图像存储在 Android Studio 的 SQLite 数据库中 [英] I want to store my Image in SQLite database in Android Studio

查看:97
本文介绍了我想将我的图像存储在 Android Studio 的 SQLite 数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在网上看到了很多资源,通常是关于使用 blob 类型将我的图像存储在 android studio 的 SQLite 数据库表中,但我实际上尝试过,但不知道该怎么做.

So I have seen many resources online and it's usually about using blob type to store my image in SQLite database table in android studio but I actually tried that out and was not sure how to do it.

我实际上遇到了获取图像路径并存储为资源 ID 然后在我的主要活动中调用它的概念

I actually came across the concept of getting the path of the image and storing as resource id and then calling it in my main activity

这是我在检索方面所看到的(我正在开发一个测验应用程序,我需要检索数据库中存储的图像并像我的问题一样显示为 ImageView.)

This is what I have seen in terms of retrieving it (I am developing a quiz app and I need to retrieve the stored image in the database and display as an ImageView like my question.)

有什么办法可以让我知道如何将图像存储在 android studio 的 SQLite 表中,然后这样检索它.我知道 getImage() 是将被声明的 getter,资源将存储在其中可能是 SQLite put 命令,该命令通常用于使用内容值插入值

Is there any way someone can let me know how I can store the image in SQLite table in android studio and then retrieving it as such. I am aware that getImage() is the getter that will be declared in which the resource will be stored in that possibly the SQLite put command that is usually done to insert values using content values

    photoQuestion = (ImageView) findViewById(R.id.photoquestion);
    photoQuestion.setImageResource(PHOTO_QUESTION.getImage());

我的代码插入数据到表中,到目前为止我的问题(应该是图像)我一直在测试它作为字符串能够显示并查看它是否工作,但现在我想替换表的第一个字段到图像(从当前字符串改为图像数据类型)

My code to insert data into the table, so far for my question (which is supposed to be the image) i have been testing it as string to be able to display and see if it is working but now i want to replace the first field of the table to image (from current string to image datatype instead)

    private void fillQuestions() {
            PeopleModeQuestions question1 = new PeopleModeQuestions("photo of john", "Father", "Mother", 1);
            addQuestions(question1); // question will be inserted in database everytime this method is called
    
            PeopleModeQuestions question2 = new PeopleModeQuestions("photo of mary", "Father", "Mother", 2);
            addQuestions(question2);
    
            PeopleModeQuestions question3 = new PeopleModeQuestions("photo of susan", "Sister", "Friend", 1);
            addQuestions(question3);
    
            PeopleModeQuestions question4 = new PeopleModeQuestions("photo of mark", "Sister", "Friend", 2);
            addQuestions(question4);
        }
    
        private void addQuestions(PeopleModeQuestions questions) {
            ContentValues values = new ContentValues();
            // using the getter method, get questions out of the object
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_Photo, questions.getPhoto());
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_Relationship1, questions.getRelationship1());
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_Relationship2, questions.getRelationship2());
            values.put(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Column_CorrectRelationship, questions.getCorrectRelationship());
            db.insert(PeopleModeConstants.MemoryDementiaQuestions.MemoryDementia_Relatives_Table, null, values);
    }

推荐答案

您能否将图像存储在 Drawable 文件夹中?我有一个特别的应用程序,它有大约 50 张图像,并且运行良好.我知道它会变得有点笨拙,但这会是我的第一个想法吗?或者您是否有数百张图片需要提取?

Could you store the images in the Drawable folder? I've got one app in particular that has about 50 images and it's worked well doing that. I know it can get a bit unwieldy, but that would be my first thought? Or do you have hundreds of images to pull out?

以编程方式设置图像视图:

Programatically set an imageView:

myImage.setImageResource(getResources().getIdentifier("myImage3.png", "drawable", getPackageName()));

myImage.setImageResource(getResources().getIdentifier("myImage3.png", "drawable", getPackageName()));

这篇关于我想将我的图像存储在 Android Studio 的 SQLite 数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆