使用MySQLHelper从Drawable插入照片时出错

时间:2014-03-28 13:56:14

标签: java android sql sqlite android-sqlite

我目前在制作特定图片时遇到困难,在查看错误消息后,我无法解读为什么它根本没有出现。

这是LogCat:

03-28 13:45:49.552: E/SpannableStringBuilder(21649): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 13:45:49.552: E/SpannableStringBuilder(21649): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 13:45:53.047: E/SQLiteDatabase(21649): Error inserting photo=[B@433d3d88 name=Chord Name: A-7th
03-28 13:45:53.047: E/SQLiteDatabase(21649): android.database.sqlite.SQLiteConstraintException: column name is not unique (code 19)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:905)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at com.finalyearproject.MySQLiteHelper.insertCrdDetails(MySQLiteHelper.java:77)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at com.finalyearproject.ChordImages.onCreate(ChordImages.java:63)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.Activity.performCreate(Activity.java:5206)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.os.Looper.loop(Looper.java:137)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at java.lang.reflect.Method.invokeNative(Native Method)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at java.lang.reflect.Method.invoke(Method.java:511)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
03-28 13:45:53.047: E/SQLiteDatabase(21649):    at dalvik.system.NativeStart.main(Native Method)
03-28 13:45:53.182: E/SpannableStringBuilder(21649): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-28 13:45:53.182: E/SpannableStringBuilder(21649): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length

我从LogCat文件中注意到我的ChordImages.onCreate方法出错,并且在查看这些文件后我的MySQLiteHelper.getCrdDetails方法中出现了错误,我感到很遗憾#&#c 39;导致问题。只是想知道是否有人可以阅读代码并让我知道你是否能发现任何明显的东西。

ChordImages.java:

package com.finalyearproject;

import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class ChordImages extends Activity {
 private MySQLiteHelper DbHelper;

  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.chordview);

      DbHelper = new MySQLiteHelper(this);
      Chords A7th = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.a_7), "A-7th");

    /*  DbHelper = new MySQLiteHelper(this);
      Chords A_Maj = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.a_maj), "Chord: A-Major");

      DbHelper = new MySQLiteHelper(this);
      Chords A_Min = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.a_min), "Chord: A-Minor");

      DbHelper = new MySQLiteHelper(this);
      Chords B_7th = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.b_7), "Chord: B-7th");

      DbHelper = new MySQLiteHelper(this);
      Chords C_7th = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.c_7), "Chord: C-7th");

      DbHelper = new MySQLiteHelper(this);
      Chords C_Maj = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.c_maj), "Chord: C-Major");

      DbHelper = new MySQLiteHelper(this);
      Chords D_7th = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.d_7), "Chord: D-7th");

      DbHelper = new MySQLiteHelper(this);
      Chords D_Maj = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.d_maj), "Chord: D-Major");

      DbHelper = new MySQLiteHelper(this);
      Chords D_Min = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.d_min), "Chord: D-Minor");

      DbHelper = new MySQLiteHelper(this);
      Chords E_7th = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.e_7), "Chord: E-7th");

      DbHelper = new MySQLiteHelper(this);
      Chords E_Maj = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.e_maj), "Chord: E-Major");

      DbHelper = new MySQLiteHelper(this);
      Chords E_Min = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.e_min), "Chord: E-Minor");

      DbHelper = new MySQLiteHelper(this);
      Chords F_Maj = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.f_maj), "Chord: F-Major");

      DbHelper = new MySQLiteHelper(this);
      Chords G_7th = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.g_7), "Chord: G-7th");

      DbHelper = new MySQLiteHelper(this);
      Chords G_Maj = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.g_maj), "Chord: G-Major"); */


      DbHelper.open();
      DbHelper.insertCrdDetails(A7th);
      DbHelper.close();
      A7th = null;
      DbHelper.open();
      A7th = DbHelper.retriveCrdDetails();
      DbHelper.close();

  TextView crdname = (TextView) findViewById(R.id.name);
  crdname.setText(A7th.getName());

  ImageView crdphoto = (ImageView) findViewById(R.id.photo);
  crdphoto.setImageBitmap(A7th.getBitmap());
  }
}

MySQLiteHelper.java:

package com.finalyearproject;

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;

public class MySQLiteHelper {

    public static final String CRD_ID = "id";
    public static final String CRD_NAME = "name";
    public static final String CRD_PHOTO = "photo";

    private DatabaseHelper mMySQLiteHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_NAME = "ChordDB.db";
    private static final int DATABASE_VERSION = 1;

    private static final String CHORD_TABLE = "Chords";

    private static final String CREATE_CHORD_TABLE = "create table "
            + CHORD_TABLE + " (" + CRD_ID
            + " integer primary key autoincrement, " + CRD_PHOTO
            + " blob not null, " + CRD_NAME + " text not null );";


    private final Context mCtx;

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

public void onCreate(SQLiteDatabase db) {

    db.execSQL(CREATE_CHORD_TABLE);
  }

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    db.execSQL("DROP TABLE IF EXISTS " + CHORD_TABLE);
    onCreate(db);
  }
 }

public void Reset() {

    mMySQLiteHelper.onUpgrade(this.mDb, 1, 1);
 }

public MySQLiteHelper(Context ctx) {

      mCtx = ctx;
      mMySQLiteHelper = new DatabaseHelper(mCtx);
 }

public MySQLiteHelper open() throws SQLException {

    mDb = mMySQLiteHelper.getWritableDatabase();
    return this;
 }

public void close() {

    mMySQLiteHelper.close();
 }

public void insertCrdDetails(Chords chord) {

    ContentValues cv = new ContentValues();
    cv.put(CRD_PHOTO, Utility.getBytes(chord.getBitmap()));
    cv.put(CRD_NAME, chord.getName());
    mDb.insert(CHORD_TABLE, null, cv);
 }

public Chords retriveCrdDetails() throws SQLException {

    Cursor cur = mDb.query(true, CHORD_TABLE, new String[] { CRD_PHOTO, CRD_NAME }, null, null, null, null, null, null);
        if (cur.moveToFirst()) {
            byte[] blob = cur.getBlob(cur.getColumnIndex(CRD_PHOTO));
            String name = cur.getString(cur.getColumnIndex(CRD_NAME));
            cur.close();

            return new Chords(Utility.getPhoto(blob), name);
  }
        cur.close();
        return null;
 }
}

慢慢但肯定地学习如何自己处理这个问题,但自从昨晚以来我一直在研究这个问题,而且似乎无法获得我想要展示的图像。我的所有图像文件都包含在Drawable文件夹中。

0 个答案:

没有答案