【发布时间】:2016-05-14 05:46:33
【问题描述】:
您好,在我的项目中,我有一个搜索栏,用户可以在其中输入动物名称,我的应用程序将显示该动物的动画图片 (.gif)。 我目前将图像的路径保存在我的 SQLite 数据库中。我有两个问题
a) 我的数据库目前只包含 1 张图片,我该如何扩展它,我是否必须为每个图片文件重新编写代码?
b) 搜索将如何进行?它会按名称搜索图像还是我必须为图像标签创建另一个列然后按它搜索。
这是我的代码:
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ImageDatabase extends Activity{
private ImageView mImageView;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mImageView = (ImageView) findViewById(R.id.image_view);
db = openOrCreateDatabase("Image.db", Context.MODE_PRIVATE, null);
db.execSQL("Create Table if not exists tb (a blob)");
}
public void saveImage(View view){
try {
FileInputStream fis = new FileInputStream("/storage/sdcard/gif_file.gif");
byte[] image = new byte[fis.available()];
fis.read(image);
ContentValues values = new ContentValues();
values.put("a", image);
db.insert("ImageTable", null, values);
fis.close();
}
catch (IOException e){
e.printStackTrace();
}
}
public void getImage(View view){
Cursor c = db.rawQuery("select = from tb", null);
if (c.moveToNext()){
byte[] image = c.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
mImageView.setImageBitmap(bmp);
}
}
}
【问题讨论】:
标签: java android android-studio android-sqlite