Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何用图像填充SQLite db,并从中检索图像?_Java_Android_Sqlite - Fatal编程技术网

Java 如何用图像填充SQLite db,并从中检索图像?

Java 如何用图像填充SQLite db,并从中检索图像?,java,android,sqlite,Java,Android,Sqlite,我的资产文件夹中有100个图像。我想在imageview中设置一个图像,下面是四个按钮,其中有四个可能的答案与图像连接(当然是一个正确答案)。我在想,我已经做了一个类似的问答游戏,我正在使用SQLite数据库进行问答。所以,有没有一种方法可以使用sqlite数据库来实现这一点,但只有当我的问题在数据库中时,我才会放置一些术语或整数,例如1表示image_1.png,2表示image_2.png等等 或者另一种情况是用图像预填充数据库,但我不知道这是否可行,或者如何实现 有没有一种方法可以重写我的

我的资产文件夹中有100个图像。我想在imageview中设置一个图像,下面是四个按钮,其中有四个可能的答案与图像连接(当然是一个正确答案)。我在想,我已经做了一个类似的问答游戏,我正在使用SQLite数据库进行问答。所以,有没有一种方法可以使用sqlite数据库来实现这一点,但只有当我的问题在数据库中时,我才会放置一些术语或整数,例如1表示image_1.png,2表示image_2.png等等

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

有没有一种方法可以重写我的db helper来使用它?这是我的db助手和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;
         }
     }
}

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

选中此项:


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

由于您的资产中已经有图像,我建议您仅将相关图像的名称保存在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);
}

为什么要把它们放进数据库?它们已经在资产中,完全可以访问你的应用程序。@323go我同意。但有些情况下,例如使用同一数据库的跨平台应用程序,可能是一个更简单的解决方案。@wtsang02,iOS也让您捆绑文件,我希望Windows也有同样的概念。在数据库中放置静态blob通常是一个坏主意。@323go我同意,正如我所说的,这是一个简单的解决方案,也有缺点,但在某些情况下确实节省了时间。这是一个比在不占用数据库的情况下在资产文件夹中放置图像更好的解决方案吗?好的,怎么做?你能举一个例子吗,我的代码中需要更正什么才能实现这一点?