1

I am new in programming, Through permissions, I get the image from the user and after converting it to Bitmap set it to the ImageView, I also created a database and table, Now what should I do next to passed this Image and Text to SQLite and display it on recyclerView ??

activity_main_xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="text"
        android:id="@+id/text"
        />



    <ImageView
        android:id="@+id/GotImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_centerHorizontal="true"
        />


    <Button
        android:id="@+id/ImageBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="163dp"
        android:text="Choose Photo" />

    <Button
        android:id="@+id/Add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="219dp"
        android:text="Add" />



    <!--<android.support.v7.widget.RecyclerView-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_below="@id/Add"-->
        <!--android:id="@+id/recycler">-->

    <!--</android.support.v7.widget.RecyclerView>-->

</RelativeLayout>

Model Class

package com.example.muhammadrizwan.sqliterecyclerview

class Catogeries(var name:String,var Image:Byte) {

}

DbHelper

package com.example.muhammadrizwan.sqliterecyclerview

import android.content.ContentValues
import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper

class DbHelper(var ctx:Context) : SQLiteOpenHelper(ctx,"My Datbase",null,1) {
    override fun onCreate(db: SQLiteDatabase?) {

        var create_table = "CREATE TABLE CATOGERIES (_Id INTEGER PRIMARY KEY AUTOINCREMENT , NAME STRING,IMAGE BLOB)"
        db?.execSQL(create_table)
    }

    override fun onUpgrade(p0: SQLiteDatabase?, p1: Int, p2: Int) {

    }

    fun insertData(catogeries: Catogeries,db:SQLiteDatabase)
    {
        var dbHelper = this.writableDatabase
        var value = ContentValues()
        value.put("Name",catogeries.name)
        value.put("Image",catogeries.Image)
        db.insert("Catogeries",null,value)
    }

}

MainActivity

package com.example.muhammadrizwan.sqliterecyclerview

import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.Image
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import java.io.ByteArrayOutputStream

class MainActivity : AppCompatActivity() {

    private lateinit var Title: EditText
    private lateinit var imageBtn: Button
    var CameraRequestCode :Int = 200
    var GalleryRequestCode :Int = 100
    private lateinit var _addBtn: Button
    var db = DbHelper(this)


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //_Image = findViewById<ImageView>(R.id.GotImage)
        Title = findViewById<EditText>(R.id.text)
        imageBtn = findViewById<Button>(R.id.ImageBtn)
        _addBtn = findViewById<Button>(R.id.Add)


        _addBtn.setOnClickListener {

            var _title = Title.text.trim().toString()

        }
        imageBtn.setOnClickListener {
            var dialogueBox = AlertDialog.Builder(this)
            var dialogueOptions = arrayOf("Camera","Gallery")
            dialogueBox.setTitle("Make Choice")
            dialogueBox.setItems(dialogueOptions,object : DialogInterface.OnClickListener
            {
                override fun onClick(p0: DialogInterface?, p1: Int) {
                    if(dialogueOptions[p1].equals("Camera"))
                    {
                        OpenCamera()
                    }
                    if (dialogueOptions[p1].equals("Gallery"))
                    {
                        OpenGallery()
                    }

                }

            })
            dialogueBox.show()
        }
    }

    private fun OpenGallery() {
        if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED)
        {
            var GalleryIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
            startActivityForResult(GalleryIntent,GalleryRequestCode)
        }
        else
        {
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),GalleryRequestCode)
        }
    }

    private fun OpenCamera() {
        if(ContextCompat.checkSelfPermission(this,android.Manifest.permission.CAMERA)== PackageManager.PERMISSION_GRANTED)
        {
            val CameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
            startActivityForResult(CameraIntent,CameraRequestCode)
        }
        else
        {
            ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CAMERA),CameraRequestCode)
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        when(requestCode)
        {
            CameraRequestCode->
            {
                if(grantResults.size>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED )
                {
                    OpenCamera()
                }
                else
                {
                    Toast.makeText(this,"Error", Toast.LENGTH_SHORT).show()
                }
            }

            GalleryRequestCode->
            {
                if(grantResults.size>0 && grantResults[0]== PackageManager.PERMISSION_GRANTED)
                {
                    OpenGallery()
                }
                else
                {
                    Toast.makeText(this,"Error", Toast.LENGTH_SHORT).show()
                }
            }

        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when(requestCode)
        {
            CameraRequestCode->
            {
                if(resultCode== Activity.RESULT_OK)
                {

                    val img = data?.extras?.get("data")
                    GotImage.setImageBitmap(img as Bitmap)
                }
            }
            GalleryRequestCode->
            {
                if(resultCode== RESULT_OK)
                {
                    var URI = data!!.data
                    var Image = MediaStore.Images.Media.getBitmap(this.contentResolver,URI)
                    GotImage.setImageBitmap(Image)

                }
            }
        }


    }

}
Brijesh Joshi
  • 1,817
  • 1
  • 9
  • 24
  • 3
    probably it is a bad idea to store entire image in the BD. Probably it would be better to store it in the file, and put the file name into the DB – Vladyslav Matviienko Feb 05 '19 at 11:26
  • 1
    To store image use file path and store that file path into your local DB and display that image with your path itself that will be the ideal approach. – Mohit Dholakia Oct 11 '19 at 10:17

0 Answers0