【问题标题】:Image not inserting in sqlite database android图像未插入sqlite数据库android
【发布时间】:2015-10-10 14:25:51
【问题描述】:

任何人请帮助我。我正在尝试创建一个字典应用程序,我想在其中保存单词、它们的含义和每个单词的图片。我正在尝试将所有单词、图像和含义保存在 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" />

这是我的列表视图的 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();
    }

}

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

含义.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天以来,我一直被困在这里。

【问题讨论】:

  • listview 显示我字典中的所有单词。
  • 显示试图显示图像的代码。
  • 我已经编辑了我的问题,其中包括显示图像的代码

标签: java android sqlite


【解决方案1】:

理想情况下,您不应该在 SQLite 中执行此操作。 SQLite 是一个只有 250KB 大小的轻量级数据库。当涉及到字典时,SQLite 将无法满足您的要求。 尝试将其保存在服务器或内部内存中,并将 URL 或路径分别保存在数据库中。

【讨论】:

  • Ishant:但是如果我在任何其他设备上运行我的应用程序,如何从外部存储访问我的图片??
  • 您必须将其保存在内存中。应用沙箱。而且,随着图像的增加,这将需要更多内存,因此最好维护一个在线数据库。
猜你喜欢
  • 1970-01-01
  • 2017-05-15
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多