0

我正在制作一个在线应用程序,其中我使用 zxing 库开发了 QR 码扫描仪。现在,它正在将数据保存在历史记录中并生成自己的数据库。

但是我想生成我自己的数据库,并在扫描了我想要存储在我的表中的任何数据之后。

但我无法做到这一点。

请给我一些建议。我是android新手,我被卡住了。

谢谢。

4

2 回答 2

0

你可能会在你的活动中得到这样的结果

  public void onActivityResult(int requestCode, int resultCode, Intent intent) {
      IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
      if (scanResult != null) {
        // handle scan result, Save result in database 'result.getText().toString();'
      }
      // else continue with any other code you need in the method
      ...
    }
于 2013-10-22T10:34:09.727 回答
0
 <FrameLayout
                        android:id="@+id/frame_scan"
                        android:layout_width="150dip"
                        android:layout_height="100dip"
                        android:layout_gravity="center_horizontal" >

                        <include layout="@layout/capture" />
                    </FrameLayout>

在您的 main.xml 中添加上述 xml 代码并扩展 CaptureActivity,如下所示:

public class ScanCard extends CaptureActivity {

@Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.scan_card);
}

if you using zxing as a library and extend the CaptureActivity, you will get the result as follows :

    @Override
    public void handleDecode(Result rawResult, Bitmap barcode) {
        // TODO Auto-generated method stub
        super.handleDecode(rawResult, barcode);
         
        Toast.makeText(
                ScanCard.this,
                "Results : "
                        + rawResult.getText().toString(), Toast.LENGTH_SHORT)
                .show();

         
    }

}

--

// 从上述方法中获取扫描的值并将其保存在数据库中,如下所示:

MultiMediaDB mDB = new MultiMediaDB();
mDB.open();

--

您插入数据库的代码:

说前:

mDB.insertMediaURL("scanned results");
mDB.close();

并且不要忘记关闭数据库。否则可能会抛出非法状态异常

这是带有值的简单数据库类。

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

public class MultiMediaDB {

public static final String PHOTO_URL = "photo";
public static final String KEY_ID = "_id";
private final Context mCtx;
private DatabaseHelper mDbHelper;
public SQLiteDatabase mDb;
private static final String TAG = "Multimedia DB ";

// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "MultiMediaDB.db";
// Multimedia table name
private static final String MUTLIMEDIA_TABLE = "MultiMediaTable";

private static final String CREATE_MUTLIMEDIA_TABLE = " CREATE TABLE if not exists "
        + MUTLIMEDIA_TABLE
        + "("
        + KEY_ID
        + " integer PRIMARY KEY autoincrement,"
        + PHOTO_URL
        + " TEXT "
        + ")";

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_MUTLIMEDIA_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + MUTLIMEDIA_TABLE);
        onCreate(db);
    }
}

public MultiMediaDB(Context ctx) {
    this.mCtx = ctx;
}

public MultiMediaDB open() throws SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

public void close() {
    if (mDbHelper != null) {
        mDbHelper.close();
    }
}

public SQLiteDatabase getDbName(Context ctx) {
    return mDb;
}

// Delete all the candidates
public boolean deleteAllCandidates() {
    int doneDelete = 0;
    doneDelete = mDb.delete(MUTLIMEDIA_TABLE, null, null);
    Log.i(TAG, Integer.toString(doneDelete));
    return doneDelete > 0;
}

// Fetching all the candidates from the database
public Cursor fetchAllCandidates() {
    Cursor mCursor = mDb.query(MUTLIMEDIA_TABLE, new String[] { KEY_ID,
            PHOTO_URL }, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

// Deleting single candidate
public void deleteSingleRow(int row_id) {
    mDb.delete(MUTLIMEDIA_TABLE, KEY_ID + " = ?",
            new String[] { String.valueOf(row_id) });
}

// Get All PHOTO_URL :

public Cursor getAllPhotURL(String string) {

    Cursor mCursor = null;
    mCursor = mDb.rawQuery(" SELECT  " + PHOTO_URL + "  FROM "
            + MUTLIMEDIA_TABLE, null);
    return mCursor;
}

// Check the database for the photo url available in database.

public boolean isPhotoAvailable(String photoUrl) {
    Cursor c = mDb.rawQuery("SELECT " + PHOTO_URL + " FROM "
            + MUTLIMEDIA_TABLE + " WHERE " + PHOTO_URL + " = " + "'"
            + photoUrl + "'", null);
    if (c.getCount() == 0)
        return false;
    else
        return true;
}

public long insertMediaURL(String mPhotURL) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(PHOTO_URL, mPhotURL);
    Log.d("The Values are ", " Inserted" + mPhotURL);
    return mDb.insert(MUTLIMEDIA_TABLE, null, initialValues);
}
}

让我知道是否有任何问题

于 2013-10-22T10:34:48.713 回答