如何在成功的图像跟踪后返回特定的SQLite行

时间:2014-10-02 09:52:43

标签: android image sqlite

我的问题是一个完全具体的问题。

我使用增强现实平台,通过智能手机相机实现图像识别。 我已经存储在我的Android SQLite表格中的特定行,这些行有关我存储在手机中的一些照片。

在我成功进行图像跟踪(我使用图像识别进行导航)之后,我想根据我公认的图像在xml存档上显示这些数据,但我无法想出如何做到这一点。到目前为止还没有发现一些类似于我的问题的代码。

我如何将我识别的图像与我在SQLite表中安装的每个图像特定数据相对应?

1 个答案:

答案 0 :(得分:0)

- >问题已解决。它像魅力一样工作并检索特定的SQLite行< -

我在DataBaseHandler.java中插入了一个名为getContact的方法来检索特定的行,并将其称为我的MainActivity。

// ---retrieves a particular contact, written in DataBaseHandler---
    public Cursor getContact(String rowId) throws SQLException {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_IMAGEID, KEY_TITLE, KEY_INFO }, KEY_IMAGEID + " LIKE '"
                + rowId + "'", null, null, null, null, null);

        if (mCursor != null) {
            if (mCursor.getCount() < 1) {
                Log.e("GetContact", "cursor empty for " + KEY_IMAGEID + "=\""
                        + rowId + "\"");

            }

        } else
            Log.e("GetContact", "cursor null");
        return mCursor;
    }

// getContact方法的主要活动调用

public void DisplayContact(Cursor c) {
            Toast.makeText(
                    this,
                    "Image_ID:" + c.getString(0) + "\n" + "Title:" + c.getString(1)
                            + "\n" + "Info:" + c.getString(2), Toast.LENGTH_LONG)
                    .show();

DataBaseHandler db;
        db = new DataBaseHandler(getApplicationContext());// Your table is
                                                            // created now

        db.open();

        Cursor c = db.getContact("24");
        System.out.println("Cursor size " + c.getCount());
        if (c.moveToFirst()) {

            DisplayContact(c); // edo kalei ti methodo DisplayContact pou
                                // tiponei tin
                                // 2i grammi tou SQLite Table
        } else
            Toast.makeText(this, "No contact found", Toast.LENGTH_LONG).show();
        db.close();
        }

我的初始DataBaseHandler.java是:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;


    public class DataBaseHandler extends SQLiteOpenHelper {

    // Database Version
    private static final int DATABASE_VERSION = 2;
    // Database Name
    private static final String DATABASE_NAME = "My_SQLite_Database.db";

    //constructor for this class
    public DataBaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating the Table when the class is executed    
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_TABLE = "CREATE TABLE " + "My_SQLite_Database" + "("
                + "Image_ID" + " TEXT,"
                + "Image" + " TEXT," + "Title" + " TEXT," + "Info" + " TEXT" + ")";
        db.execSQL(CREATE_TABLE);//executing the create table query
    }

    //for inserting data into this table write the following method
      public void addData(String field1, String field2, String field3, String field4) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put("Image_ID", field1); 
        values.put("Image", field2); 
        values.put("Title", field3); 
        values.put("Info",  field4); 

        // Inserting Row
        db.insert("My_SQLite_Database", null, values);
        db.close(); // Closing database connection
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.w(DataBaseHandler.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + "My_SQLite_Database");
            onCreate(db);
    }
}