数据库包含图像,无法从数据库(Android应用程序)调用这些图像

时间:2014-04-03 12:52:49

标签: java android database image sqlite

所以我创建了一个数据库,其中包含与数据库的每个条目相关联的ID,名称和位图图片。数据库输入如下所示:

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());
  }
}

基本上我在这里的最后一节我希望它找到'名称'在我的用户界面上使用TextView并为其分配“A7th'数据库中的名称和后面的代码也是为了获取相应的图像。我还有另外几个Java文件可能有助于检测我的图像没有出现的原因。

Chords.java:

package com.finalyearproject;

import android.graphics.Bitmap;

public class Chords {

    private int id;
    private String chord;
    private Bitmap bitmap;

    public Chords(){}

    public Chords(Bitmap picture, String chord) {
        super();
        this.chord = chord;
        this.bitmap = picture;
    }

    @Override
    public String toString() {
        return "Chords [id=" + id + ", chord=" + chord + ", picture=" + bitmap + "]";
    }

    public String getName() {
        return "Chord Name: " + chord;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }
}

这包含应该获取与每个条目关联的名称和图像的2种方法。我认为这部分工作正常。

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;
 }
}

这是创建我相信的数据库的文件,它与我的Utility.java文件相关联,该文件将图像更改为ByteArray,然后在他们被调用的位置将其更改回来。效用文件位于

下面

Utility.java:

package com.finalyearproject;

import java.io.ByteArrayOutputStream;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;

public class Utility {
 // convert from bitmap to byte array
    public static byte[] getBytes(Bitmap bitmap) 
        {
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.PNG, 0, stream);
            return stream.toByteArray();
        }

  // convert from byte array to bitmap
 public static Bitmap getPhoto(byte[] image) {

     return BitmapFactory.decodeByteArray(image, 0, image.length);
    }
}

我将每个有问题的图像保存到我的Drawable文件夹中,并且每个图像都有一个唯一的名称,但是当我在应用程序本身中打开ChordImages.java活动时,它不会将图像显示到第一个图像。我想现在只显示一个图像,然后我将继续使用Spinner让我更改实际要显示的图像,任何人都可以看到我的代码中的任何blatent问题都会阻止加载图片。我将在下面附上LogCat错误日志:

logcat的:

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

我可以看到插入照片的错误' LogCat文件中的消息,但我似乎无法破译导致问题的原因!任何建议表示赞赏。

0 个答案:

没有答案