Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add Image and text in Sqlite and Display it in recyclerView with kotlin

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.84k
    Answer it

    I want to take image and text from user and add in Sqlite and display it in recyclerView with kotin 

     

    Let me show what I have done so far 

     

    >activity_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)

                    }
                }
            }

        }

    }
     

     

     

     

 0 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: