Java 数据库包括图像、can和x27;t从数据库调用这些图像(Android应用程序)
因此,我创建了一个数据库,其中包含一个ID、名称和位图图片,与数据库的每个条目关联。数据库输入如下所示:Java 数据库包括图像、can和x27;t从数据库调用这些图像(Android应用程序),java,android,database,image,sqlite,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
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());
}
}
基本上,这里的最后一部分我想让它在我的UI上找到'name'文本视图,并从数据库中为其分配'a7'名称的值,接下来的代码也是获取相应的图像。我还有另外两个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;
}
}
这包含两个方法,它们应该获取与每个条目关联的名称和图像。我认为这部分工作正常
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活动时,它不会将图像显示到第一个图像。我希望现在只显示一个图像,然后我将使用微调器来更改实际要显示的图像,有人能看到我的代码中有任何明显的问题,从而阻止图像加载。我将在下面附上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文件中看到“错误插入照片”消息,但我似乎无法解释导致问题的原因!感谢您的任何建议。仍在继续将图像保存到数据库中吗?;)就你的错误而言,我唯一能建议的是转到设置->应用程序并找到你的应用程序。强制关闭数据库,然后删除数据-这将确保从头开始重新创建数据库。如果这不起作用,那么我不确定问题出在哪里。您确实有一些非常奇怪的编码方法-使用
DbHelper=newmysqlitehelper创建一个新的helper(这个)每次创建一个新的和弦
实例之前,都要使用code>。@Squonk将尝试一下,看看会发生什么,如果我能让图像显示出来,那么我将尝试将它与微调器链接起来。。。我可能有一种模糊的编码方法,因为我是Android开发的初学者!好的,我成功地对数据库中的内容进行了排序,并显示了一个图像,我现在还有一个微调器,可以根据所选内容更改文本。。。我想我会发布一个新的问题来寻求帮助!你的评论帮我找到了正确的地方@Squonk非常感谢你,巴德!