Hi in my project I have a Search bar in which the user will type an animal name and my application will display the an animated picture of that animal(.gif). Im currently saving the paths of the image in my SQLite database. There are two questions which I have
a) My database currently comprises of just 1 image how can I expand it, do I have to write the code again for each image file?
b) How will the search work? will it search the image by name or do I have to make another Column for image tag and then search by it.
Here's my code:
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);
}
}
}