Blog purpose for android basic example for android app developer. any query please my contact

Thursday, 30 April 2020

how to upload image in sqlite database in android

In this an article today learn how to upload image in sqlite database in android. Sqlite using a storage data. Then save image blob formatting. Follow the  method image upload and retrieve for sqlite database.

 

Select Image method:

{

        Intent intent = new Intent(Intent.ACTION_PICK,

 

                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

 

        startActivityForResult(intent,0);

    }

 

 

Upload Image Method:

 

public void uploadImage(View view)

    {

        db=mdb.getWritableDatabase();

        ContentValues cv = new ContentValues();

        cv.put("image", img);

        db.insert("tableimage", null, cv);

        imageView.setAlpha(0);

        Toast.makeText(this, "inserted successfully", Toast.LENGTH_SHORT).show();

 

    }

    public void retImage(View view)

    {

        String[] col={"image"};

        db=mdb.getReadableDatabase();

        c=db.query("tableimage", col, null, null, null, null, null);

        if(c!=null){

            c.moveToFirst();

            do{

                img=c.getBlob(c.getColumnIndex("image"));

            }while(c.moveToNext());

        }

        Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length);

 

        imageview2.setImageBitmap(b1);

        Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();

    }

 


add permission on AndroidManifest.xml file


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

Follow the full source code how to upload image in sqlite database in android


1.MainActivity.java:

package com.akash.sqliteiamge;


import android.app.Activity;

import android.content.ContentValues;

import android.content.Intent;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.View;

import android.widget.ImageView;

import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;


import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.IOException;


public class MainActivity extends AppCompatActivity {

    ImageView imageView,imageview2;

    Bitmap bitmap = null;

    byte img[];

    private MyDataBase mdb=null;

    private SQLiteDatabase db=null;

    private Cursor c=null;

    private static final String DATABASE_NAME = "ImageDb.db";

    public static final int DATABASE_VERSION = 1;


    @Override    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        imageView = (ImageView) findViewById(R.id.imageView);

        imageview2 = (ImageView) findViewById(R.id.imageView2);


        mdb=new MyDataBase(getApplicationContext(), DATABASE_NAME,null,


                DATABASE_VERSION);


    }

    public void selectImage(View view)

    {

        Intent intent = new Intent(Intent.ACTION_PICK,


                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);


        startActivityForResult(intent,0);

    }


    @Override


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {


        if(resultCode== Activity.RESULT_OK && data !=null)

        {

            Uri selectedImage = data.getData();

            try        {

                bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),


                        selectedImage);

                ByteArrayOutputStream bos = new ByteArrayOutputStream();

                bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);

                img = bos.toByteArray();

                imageView.setImageBitmap(bitmap);

            } catch (FileNotFoundException e) {

                e.printStackTrace();

            } catch (IOException e) {

                e.printStackTrace();

            }

        }


    }


    @SuppressWarnings("deprecation")

    public void uploadImage(View view)

    {

        db=mdb.getWritableDatabase();

        ContentValues cv = new ContentValues();

        cv.put("image", img);

        db.insert("tableimage", null, cv);

        imageView.setAlpha(0);

        Toast.makeText(this, "inserted successfully", Toast.LENGTH_SHORT).show();


    }

    public void retImage(View view)

    {

        String[] col={"image"};

        db=mdb.getReadableDatabase();

        c=db.query("tableimage", col, null, null, null, null, null);

        if(c!=null){

            c.moveToFirst();

            do{

                img=c.getBlob(c.getColumnIndex("image"));

            }while(c.moveToNext());

        }

        Bitmap b1=BitmapFactory.decodeByteArray(img, 0, img.length);


        imageview2.setImageBitmap(b1);

        Toast.makeText(this, "Retrive successfully", Toast.LENGTH_SHORT).show();

    }

}




2.MyDataBase.java:


package com.akash.sqliteiamge;


import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;


public class MyDataBase extends SQLiteOpenHelper {


    public MyDataBase(Context context, String dbname,


                      SQLiteDatabase.CursorFactory factory, int dbversion) {

        super(context, dbname, factory, dbversion);

    }


    @Override    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table tableimage(image blob);");

    }


    @Override



    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


    }


}




3.activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <RelativeLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:layout_alignParentTop="true"

        android:layout_alignParentStart="true">


        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Select Image"

            android:id="@+id/btnSelect"

            android:onClick="selectImage"

            android:layout_alignTop="@+id/btnUpload"

            android:layout_alignParentStart="true" />


        <ImageView

            android:layout_width="wrap_content"

            android:layout_height="150dp"

            android:id="@+id/imageView"

            android:src="@mipmap/ic_launcher"

            android:layout_below="@+id/btnUpload"

            android:layout_centerHorizontal="true" />


        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Upload  Image"

            android:id="@+id/btnUpload"

            android:onClick="uploadImage"

            android:layout_marginTop="0dp"

            android:layout_alignParentTop="true"

            android:layout_alignParentEnd="true" />


        <Button

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="Retrieve Image"

            android:id="@+id/button"

            android:onClick="retImage"

            android:layout_below="@+id/imageView"

            android:layout_centerHorizontal="true"

            android:layout_marginTop="67dp" />


        <ImageView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:id="@+id/imageView2"

            android:src="@mipmap/ic_launcher"

            android:layout_alignParentBottom="true"

            android:layout_alignStart="@+id/imageView"

            android:layout_marginTop="300dp" />

    </RelativeLayout>

</ScrollView>




4.AndroidManifest.xml:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akash.sqliteiamge">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



5.Output:

  1. how to upload image in sqlite database in android




how to upload image in sqlite database in android


how to upload image in sqlite database in android





 

 


No comments:

Post a Comment