Kalagar
Kalagar

Reputation: 379

How to take pic with camera and save it to database and show in listView in Android?

I want to create an inventory app for my store. I created an activity for get Product-Name and buy-price and sell-price after that take a pic of product and save theme to ListView. I'm using Sqlite. I can't save these.

This is my code to get info:

package com.kalagar.warehouse;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class AddNew extends Activity implements View.OnClickListener {

    ImageButton ib;
    Button b;
    ImageView iv;
    Intent i;
    final static int CameraData = 0;
    Bitmap bmp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnew);
        startAddNew();
    }

    private void startAddNew() {
        ib = (ImageButton) findViewById(R.id.ibTakePic);
        b = (Button) findViewById(R.id.bSave);
        iv = (ImageView) findViewById(R.id.ivPic);
        b.setOnClickListener(this);
        ib.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ibTakePic:
            i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(i, CameraData);
            break;

        case R.id.bSave:
            break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) data.getExtras().get("data");
            iv.setImageBitmap(bmp);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
        }
    }

}

This is My Database :

package com.kalagar.warehouse;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DataBase {

    @SuppressWarnings("unused")
    private static class WareHouseDdbHelper extends SQLiteOpenHelper {

        private static final String LOGTAG = "WAREHOUSE";

        static final String DATABASE_TABLE = "WareHouse";
        static final int DATABASE_VERSION = 1;
        static final String DATABASE_NAME = "SellList";

        static final String ROW_ID = "_id";
        static final String ROW_NAME = "nameOfObject";
        static final String ROW_KHARID = "ghBuy";
        static final String ROW_FOROUSH = "ghSell";
        static final String ROW_PICTURE = "picture";

        private static final String TABLE_CREATE = "CREATE TABLE "
                + DATABASE_TABLE + " (" + ROW_ID
                + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ROW_NAME + " TEXT, "
                + ROW_KHARID + " NUMERIC, " + ROW_FOROUSH + " NUMERIC, "
                + ROW_PICTURE + " TEXT " + ")";

        public WareHouseDdbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TABLE_CREATE);
            Log.i(LOGTAG, "Table has been create");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
            onCreate(db);
        }
    }
}

Upvotes: 4

Views: 6281

Answers (2)

Sinh Phan
Sinh Phan

Reputation: 1266

You can try references this lib. After you take a picture from camera, write this image to storage, then you can got the path of image. Now, you can put the path into sqlite. PS: see this class of lib

Upvotes: 1

Ankit Popli
Ankit Popli

Reputation: 2837

Firstly, you should go through some tutorials on SQLiteOpenHelpers. Here's one for you.

Once you are done with this, write your own OpenHelper with some basic methods like saveImage() and getListOfImages() etc. I'm sure you'll be able to figure these out according to your needs.

Now, Let's get to saving an Image: Once you have received the bitmap in onActivityResult you can save it using the following code:

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
File pictureFile = new File(PATH_IMAGES, "IMG_" + timeStamp + ".jpg");
FileOutputStream fos = new FileOutputStream(pictureFile);
bitmap.compress(CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();

Once you have the image written to the disk, you can get the absolute path of image an save it in your database with the help of open helper you created like this:

String imagePath = pictureFile.getAbsolutePath();
yourImageOpenHelper.insertImage(imagePath);

The next step is to retrieve the list of images and display it using ListView or GridView whatever suits your needs.

Here you need to get all the images saved in your database in a list or array. Once you have the list you need to check whether the images actually exists? You might think they'll always exists, but what if someone deleted the image manually? For that here's a simple solution

Finally, you need to worry about is java.lang.OutOfMemoryError while loading images into the ListView. You might wanna have a look at Displaying Bitmaps Efficiently if your implementing everything yourself.

If you don't have time to learn or if you are not ready take up the challenge, you can always use libraries. Here's one library.

I would recommend you not use any library, as it will give you exposure to the basics of Android. Rest its obviously up to you.

Hope this helps.. :)

Upvotes: 5

Related Questions