簡體   English   中英

圖像未插入SQLite數據庫Android中

[英]Image not inserting in sqlite database 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_含義.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" />

這是我的列表視圖的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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM