存储和检索图像路径从Android应用程序文件夹On Pre Created SQLite Database&将图像设置为图像视图

时间:2013-07-10 22:05:13

标签: android image sqlite android-sqlite xml-drawable

我目前正在尝试进行一个简单的应用测验,每个问题附有4个答案的图像,所以我有大约100个++图像存储在数据库中

到目前为止,我可以通过本教程了解如何在每个问题上没有图像的情况下提出问题:Chuck Application Part 1

我发现存储图像路径的方法是通过下面的链接硬编码SQLite数据库中文本字段的路径

Saving resources path in SQLite

根据上面的链接,他说他通过在SQLite数据库中的文本字段中键入“R.drawable.picture1”来保存图像的引用,

如果是真的,这是否正确(通过将图像保存在可绘制文件夹中并将路径写入文本字段)?如果不是那么这样做的正确方法是什么,

如何构建图像路径(设置setter和getter方法)以从数据库中获取字段?

如何设置imageview的路径?

任何帮助将不胜感激,谢谢你提前

更新

所以我从另一个链接学到了如何使用字符串作为参考,我编辑了下面的字符串,其中ImageI.getImage1();是指我的数据库中包含我的图像名称

的行
private void showImage() {
        String imagea = ImageI.getImage1();
        int imageResource = getResources().getIdentifier(imagea , "drawable", context.getPackageName());
        ImageView gambar = (ImageView) findViewById(R.id.imagetest);
         Drawable image = getResources().getDrawable(imageResource);
         gambar.setImageDrawable(image);
    }

但是我总是靠近一个力量,我做错了哪里? 到目前为止,我已经尝试在我的数据库行中写作

R.Drawable.picture
Drawable/picture
picture

但没有任何作用......

这是我的查询

    public List<ImageQuestions> getImageQuestionSet(int ingroup, int numQ) {
        List<ImageQuestions> questionSet = new ArrayList<ImageQuestions>();
        Cursor c = myDataBase.rawQuery("SELECT * FROM IMAGETEST WHERE INGROUP=" + ingroup +
                " ORDER BY RANDOM() LIMIT " + numQ, null);
         while (c.moveToNext()){      
                //Log.d("IMAGEQUESTIONS", "Images Found in DB: " + c.getString(1));
               ImageQuestions I = new ImageQuestions();
               I.setQuestion(c.getString(1));
               I.setImage1(c.getString(2));
               I.setAnswer(c.getString(3));
               I.setOption1(c.getString(4));
               I.setOption2(c.getString(5));
               I.setGroup(ingroup);
               questionSet.add(I);
           }         
           return questionSet;
        }
}

这是我的日志猫

07-18 03:28:41.283: W/dalvikvm(647): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-18 03:28:41.293: E/AndroidRuntime(647): FATAL EXCEPTION: main
07-18 03:28:41.293: E/AndroidRuntime(647): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
07-18 03:28:41.293: E/AndroidRuntime(647):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
07-18 03:28:41.293: E/AndroidRuntime(647):  at java.util.ArrayList.get(ArrayList.java:311)
07-18 03:28:41.293: E/AndroidRuntime(647):  at learning.everything.quiz.GamePlayI.getNextQuestion(GamePlayI.java:113)
07-18 03:28:41.293: E/AndroidRuntime(647):  at learning.everything.BigAss.onCreate(BigAss.java:32)
07-18 03:28:41.293: E/AndroidRuntime(647):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-18 03:28:41.293: E/AndroidRuntime(647):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)  

我意识到它是由数据库中的arraylist引起的,所以这是我的GamePlayI类

    public class GamePlayI {

    private List<ImageQuestions> questions = new ArrayList<ImageQuestions>();   

    public void setQuestions(List<ImageQuestions> questions) {
        this.questions = questions;
        }


    public void addQuestions(ImageQuestions I) {
        this.questions.add(I);
    }


    public List<ImageQuestions> getQuestions() {
        return questions;
    }


    public ImageQuestions getNextQuestion() {

        //get the question 
        //below is line 113\\
        ImageQuestions next = questions.get(this.getRound());
        //update the round number to the next round
        this.setRound(this.getRound()+1);
        return next;

    }

}

    public void setNumRounds(int numRounds) {
    this.numRounds = numRounds;
        }

    public int getNumRounds() {
    return numRounds;
}

1 个答案:

答案 0 :(得分:-1)

我只会在db中保存图像名称,然后使用context.getResources().getIdentifier()获取资源标识符。查看this类似问题。