0

我的资产文件夹中有 100 张图片。我想将图像设置为图像视图,并在四个按钮下方设置 4 个可能的答案来连接图像(当然是一个正确的)。我在想什么,我已经完成了一个类似的问答游戏,我正在使用 SQLite 数据库进行问答。那么,有没有一种方法可以为此使用 sqlite 数据库,但只有在我的问题在数据库中时,我才会放置一些术语或整数,例如 image_1.png 为 1,image_2.png 为 2 等等。

或者另一种情况是用图像预填充数据库,但我不知道这是否可能或如何去做。

有没有办法重写我的数据库助手来使用它?这是我的数据库助手和 testAdapter:

public class DataBaseHelper extends SQLiteOpenHelper
{
   private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
    //destination path (location) of our database on device
    private static String DB_PATH = "/data/data/rs.androidaplikacijekvizopstekulture/databases/"; 
    private static String DB_NAME ="pitanja.sqlite";// Database name
    private static SQLiteDatabase mDataBase; 
    private final Context mContext;
    private static final String KEY_ID = "_ID";
    private static final String TABLE_NAME = "tblPitanja";

    public DataBaseHelper(Context mojContext) 
    {
        super(mojContext, DB_NAME, null, 1);// 1? its Database Version
        DB_PATH = mojContext.getApplicationInfo().dataDir + "/databases/";
        this.mContext = mojContext;
    }

    public void createDataBase() throws IOException
    {
    //If database not exists copy it from the assets


        this.getReadableDatabase();
        this.close();
        try 
        {
            //Copy the database from assests
            copyDataBase();
            Log.e(TAG, "createDatabase database created");
        } 
        catch (IOException mIOException) 
        {
            throw new Error("ErrorCopyingDataBase");
        }
    }
    /*Check that the database exists here: /data/data/your package/databases/Da Name
    private boolean checkDataBase()
    {
        File dbFile = new File(DB_PATH + DB_NAME);
        //Log.v("dbFile", dbFile + "   "+ dbFile.exists());
        return dbFile.exists();
    }
    */

    //Copy the database from assets
    private void copyDataBase() throws IOException
    {
        InputStream mInput = mContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream mOutput = new FileOutputStream(outFileName);
        byte[] mBuffer = new byte[1024];
        int mLength;
        while ((mLength = mInput.read(mBuffer))>0)
        {
            mOutput.write(mBuffer, 0, mLength);
        }
        mOutput.flush();
        mOutput.close();
        mInput.close();
    }

    //Open the database, so we can query it
    public boolean openDataBase() throws SQLException
    {
        String mPath = DB_PATH + DB_NAME;
        //Log.v("mPath", mPath);
        mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
        //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        return mDataBase != null;
    }
    @Override
    public void close() 
    {
        if(mDataBase != null)
            mDataBase.close();
        super.close();
    }

    @Override
    public void onCreate(SQLiteDatabase arg0) {
        }

    @Override
    public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
        Log.w("DataBaseHelper", "Upgrading database!!!!!");
          onCreate(arg0);

    }

}

这是testAdaper:

public class TestAdapter 
{
    protected static final String TAG = "DataAdapter";

    private final Context mContext;
    private SQLiteDatabase mDb;
    private DataBaseHelper mDbHelper;

    public TestAdapter(Context context) 
    {
        this.mContext = context;
        mDbHelper = new DataBaseHelper(mContext);
    }

    public TestAdapter createDatabase() throws SQLException 
    {
        try 
        {
            mDbHelper.createDataBase();
        } 
        catch (IOException mIOException) 
        {
            Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase");
            throw new Error("UnableToCreateDatabase");
        }
        return this;
    }

    public TestAdapter open() throws SQLException 
    {
        try 
        {
            mDbHelper.openDataBase();
            mDbHelper.close();
            mDb = mDbHelper.getReadableDatabase();
        } 
        catch (SQLException mSQLException) 
        {
            Log.e(TAG, "open >>"+ mSQLException.toString());
            throw mSQLException;
        }
        return this;
    }

    public void close() 
    {
        mDbHelper.close();
    }

     public Cursor getTestData(String whereClause)
     {;
         try
         {
             String sql ="SELECT * FROM tblPitanja WHERE 1 = 1 " + whereClause + " ORDER BY RANDOM() LIMIT 1";

             Cursor mCur = mDb.rawQuery(sql, null);
             if (mCur!=null)
             {
                mCur.moveToNext();
             }
             return mCur;
         }
         catch (SQLException mSQLException) 
         {
             Log.e(TAG, "getTestData >>"+ mSQLException.toString());
             throw mSQLException;
         }
     }
}
4

2 回答 2

1

您可以使用数据类型将图像保存在 sqlite 中blob。使用 SQLite 管理器(firefox 插件)在计算机上预填充数据库。然后你只需要 assets 文件夹中的 db 文件,而不是那 100 张图片!

检查这个: 如何将图像存储为 Sqlite 中的 blob 以及如何检索它?

另一种方法(更快)是将图像存储在外部文件夹中,然后在数据库中输入其 URL。

于 2013-03-30T02:39:50.547 回答
0

由于您已经在资产中有图像,因此我建议您仅将关联图像的名称保存在 sqlite 中。我更喜欢总是避免在 sqlite 中保存 blob。您始终可以获取正确的名称并将其直接从数据库加载到 imageview 中。

编辑:您的代码可以像这样简单。

Cursor getname = db.rawQuery("Select * from table_name where QuestionID = ...");
if(getname.movetofirst())
{
String drawablename = getname.getString(getname.getColumnIndex("PicName"));

ImageView iw= (ImageView)findViewById(R.id.imageView1);  
int resID = getResources().getIdentifier(drawableName, "drawable",  getPackageName());
iw.setImageResource(resID);
}
于 2013-03-30T07:08:26.430 回答