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

I want to create an inventory application for my store. I created an action to get Product-Name and buy-price and sell-price, then take a picture of the product and save the theme in ListView. I am using Sqlite. I can’t save them.

This is my code for getting 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);
        }
    }
}
+4
source share
2 answers

First, you need to go through some SQLiteOpenHelpers tutorials. Here is one for you.

, OpenHelper , saveImage() getListOfImages() .. , .

: , onActivityResult, , :

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

, , , , :

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

- ListView GridView, .

, , . , , ? , , , - ?

, java.lang.OutOfMemoryError ListView. , .

, . .

- , Android. , , .

, ..:)

+4

lib. , , , . sqlite. PS: . lib

+1

Source: https://habr.com/ru/post/1527656/


All Articles