添加了包含图像的数据库......似乎无法从活动数据库中调用图像

时间:2014-03-27 20:02:24

标签: java android database image

昨天有关于图片尺寸的问题,并且在此处有人的建议后及时解决了。这个问题给我带来了更多问题......

基本上我已经进一步开发了我的应用程序,现在有一个名为" ChordDB"当我点击这个按钮时,我希望它加载我的' ChordImages.java'活动并显示'姓名'和'图像'通过某种下拉指定的和弦?

这是我第一次在Android环境中使用数据库,所以我在这里的舒适区域之外;我知道我目前在我的数据库中只有一个实例用于' C'和弦我只是在寻找如何解决这个问题的指导,如果我在那里有超过1个项目的话。欢迎任何虚拟的例子!

这是我的' 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 chord_One = new Chords(BitmapFactory.decodeResource(getResources(), R.drawable.tuner_ic), "Chord C");

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

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

  ImageView crdphoto = (ImageView) findViewById(R.id.photo);
  crdphoto.setImageBitmap(chord_One.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 unique );";


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

我的' Chords.java '文件:

package com.finalyearproject;

import android.graphics.Bitmap;

public class Chords {

    private int id;
    private String chord;
    private Bitmap picture;

    public Chords(){}

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

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

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

    public Bitmap getBitmap() {
        return picture;
    }
}

0 个答案:

没有答案