镜像不插入sqlite数据库android



谁来帮帮我。我正在尝试创建一个字典应用程序,我想保存单词,它们的含义和图片与每个单词。我试图保存所有的单词,图像和意义在sqlite数据库。问题是,当我点击任何单词在列表视图它显示我的单词和定义,但图像字段是空白的。这是我的类,我正在创建我的数据库。

    public class DictionaryDataBase extends SQLiteOpenHelper {

    byte[][] img = {
            DbBitmapUtility.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)),
            DbBitmapUtility.getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)),
            DbBitmapUtility
                    .getBytes(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)) };
    public DictionaryDataBase(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "CREATE TABLE Dict_Table (id INTEGER PRIMARY KEY, word TEXT NOT NULL, definition TEXT NOT NULL, image BLOB NOT NULL);");
        try {
            new DictionaryDB(context).addEntry(words, def, db, img);
        } catch (Exception e) {
            Log.e("Error", e.toString());
        }
    }

,这是添加条目的方法:

   public void addEntry(String[] word, String[] def, SQLiteDatabase db, byte[][] img) throws SQLiteException {
    for (int i = 0; i < word.length; i++) {
        db.execSQL("INSERT INTO Dict_Table VALUES (" + i + 1 + ", '" + word[i] + "', '" + def[i] + "', '" + img[i]
                + "');");
    }

这是我用来获取图像的方法:

    public byte[] Getimage() throws SQLException {
    dbhelper = new DictionaryDataBase(context);
    SQLiteDatabase db = dbhelper.getReadableDatabase();
    Cursor res = db.rawQuery("SELECT * FROM Dict_Table WHERE word='" + MainActivity.item + "'", null);//MainActivity.item is the item which was clicked in the list view
    res.moveToFirst();
    byte[] img = res.getBlob(res.getColumnIndex(IMG_COL));
    if (!res.isClosed()) {
        res.close();
    }
    return img;
}

DbBitmapUtility.java:

   public class DbBitmapUtility {
// 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 getImage(byte[] image) {
    return BitmapFactory.decodeByteArray(image, 0, image.length);
}
}

activity_meaning.xml(这是我的单词定义和图像的布局文件):

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.shehryar.dictionary.Meaning"
android:background="#2196F3" >
<TextView
    android:id="@+id/tvword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Word"
    android:textStyle="bold"
    android:textSize="20dip"
    android:textColor="@android:color/white" />
<TextView
    android:id="@+id/tvdefinition"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvword"
    android:layout_below="@+id/tvword"
    android:layout_marginTop="14dp"
    android:text="Definition" 
    android:textColor="@android:color/white"/>
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvdefinition"
    android:layout_below="@+id/tvdefinition"
    android:layout_marginTop="30dp"
    android:src="@drawable/abc_btn_radio_material" />
<TextView
    android:id="@+id/tvcheck"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="40dp"
    android:text="TextView" />

这是我的listview的onitemclick方法,它显示单词列表。

        @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    // TODO Auto-generated method stub
    try {
     item =(String) parent.getItemAtPosition(position);
     String def = db.GetDefinition();
    img=db.Getimage();
     Bundle basket = new Bundle();
     basket.putString("word", item);
     basket.putString("def", def);
     basket.putByteArray("img", img);

     Intent a= new Intent(this,Meaning.class);
     a.putExtras(basket);
     startActivity(a);
        //Toast.makeText(this, def, Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
    }
}

,这是显示结果的意义活动:

Meaning.java:

    package com.shehryar.dictionary;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
 public class Meaning extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_meaning);
    String word,def;
    byte[] img;

    Bundle extras=getIntent().getExtras();
    word=extras.getString("word");
    def=extras.getString("def");
    img=extras.getByteArray("img");
    Bitmap image=DbBitmapUtility.getImage(img);
    TextView tvword=(TextView) findViewById(R.id.tvword);
    TextView tvdef=(TextView) findViewById(R.id.tvdefinition);
    TextView tvcheck= (TextView) findViewById(R.id.tvcheck);
    ImageView iv= (ImageView) findViewById(R.id.imageView1);
    tvword.setText(word);
    tvdef.setText(def);
    iv.setImageBitmap(image);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.meaning, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

如果你需要任何其他信息,我会提供给你,但请帮助我在这种情况下。我已经被困在这里2-3天了

理想情况下,您不应该在SQLite中这样做。SQLite是一个轻量级数据库,只有250KB大小。说到字典,SQLite不能满足你的要求。尝试保存在服务器或内存中,并分别保存URL或路径在DB中。

最新更新