0

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);
    }

}

}

1 Answers1

0

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?

Of course not, in your method that save the image, saveImage, you can pass the path of the image to save as a parameter, to make it general. FileInputStream will not have an hardcoded value but it will use this parameter. Something like this:

public void saveImage(String filePath) {
    try {
        FileInputStream fis = new FileInputStream(filePath);
        ...
    }
}

Of course do some checks to the path or manage the exception properly. I don't know the architecture of your application, please do not store the actual image in the database, but a TEXT with the path of the image.

Also, it looks like you are not using the View view parameter in the method, so you can remove it.

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.

It depends on your application: if it's possible for the users to add tags to an image or maybe the application does it automatically (i.e. a category also), then you can give the tag search possibility to the user, otherwise not. So you will simply search by name: please consider a file can have any name, completely unrelated to the actual content of the image, so you better think about another strategy to improve your search functionality.

fasteque
  • 4,309
  • 8
  • 38
  • 50
  • I have currently planned to add 150 animals in my application, users cannot add or remove them they can just read them. So I think searching by name should do it. Secondly, So my code must look something like FileInputStream fis = new FileInputStream("/storage/sdcard/Images); and within this folder will be my images so will I be able to read them this way? – HeroicJokester May 14 '16 at 11:55
  • @HeroicJokester you should provide more details about the architecture of your application: do you embed the images within the application? Do you download them from the network? Based on these points the best approach can change. – fasteque May 14 '16 at 12:49
  • Yes I have prepared all 150 gifs and i want to add them to my application by storing them in the internal or external storage of the phone. The user cant edit or change them user will simply search the name of the animal and my app will display the image with that name. Thats all the functionally there is. (PS im thinking about adding voice input too but thats for later). – HeroicJokester May 14 '16 at 13:11
  • @HeroicJokester do you plan to add them inside the application the users will download from Google Play or they will download them from a server when the application is launched? In the first case, look here: http://stackoverflow.com/questions/9563373/the-reason-for-assets-and-raw-resources-in-android. Then if the user modify one image, you may save it in the external storage, so the original image will be not overridden. Please consider, if you are using a database just to perform a search, that you can also search for files in folders without a database support. – fasteque May 14 '16 at 13:39
  • Umm lol that's exactly what I want to do I just want to take the input from the user and search the image from my storage and display him that image. If I can do it without using database please let me how. – HeroicJokester May 14 '16 at 13:49
  • You can search for files in a directory: http://stackoverflow.com/questions/15624226/java-search-for-files-in-a-directory. You can search for example related to Android too. – fasteque May 14 '16 at 14:17
  • After a lot of searching I found [this](https://examples.javacodegeeks.com/core-java/util/search-files-in-a-directory-using-futuretask-example/) pure java code what do you think about it? I'll tweak it a little bit Or should I try this [Android Code](http://javatechig.com/android/listing-all-sdcard-filenames-with-extensions-in-android) which one do you think will be the best?? – HeroicJokester May 14 '16 at 19:29