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

Thursday, 4 March 2021

how to retrieve image from sqlite database in android

 how to retrieve image from sqlite database in android 


In this Tutorial Today Learn how to retrieve image from sqlite database in android. previous tutorial how to upload image in sqlite database in android follow this Tutorial. follow the full Source Code how to retrieve image from sqlite database in android .


1.Image_retrive.java:

package com.akash.myapplication.image.retrive;


import android.app.Activity;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.database.sqlite.SQLiteOpenHelper;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.TextView;


import com.akash.myapplication.R;


import java.io.ByteArrayOutputStream;


public class Image_retrive extends Activity

{


    /* This is my MainActivity class which extends the Activity class. Activity class provides the onCreate() method so when you run your application your compiler first goes to this method to create the activity. setContentView() method is used to set the layout in your activty. */

    /** Called when the activity is first created. */

    MyDataBase myDataBase;

    SQLiteDatabase database;

    Cursor c;

    ImageView imageView;

    byte[] img, img1;

    Bitmap b;


    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_retrive);

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

        myDataBase = new MyDataBase(getApplicationContext(), "imagedata", null, 1);


        // this.deleteDatabase("imagedata");


        /*Bitmap is a binary representation in which a bit or a set of bits correspond to some part of an object such as an image. Here we use the Bitmap class which is provided by the Android system to hold the image. Here I have created the object of Bitmap class to hold the image. bitmap Factory class is used to create the object from various resources including files, streams and byteArrays.      */


        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

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

        byte[] img = bos.toByteArray();


        //getWritebleDatabese() method is used to perform write operation into the database

        database = myDataBase.getWritableDatabase();


        // content values is used to create empty set of values using the default initial size

        ContentValues cv = new ContentValues();

        cv.put("image", img);

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

        String selectQuery = "SELECT * FROM tableimage";

        c = database.rawQuery(selectQuery, null);

        if (c != null)

        {

            c.moveToFirst();

            do

            {

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

            } while (c.moveToNext());

        }

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

        imageView.setImageBitmap(b1);

    }

}


// Mydatabase class is used to create the database by extending the SqliteopenHelper class which provides onCrate() and onUpGrade() method.

class MyDataBase extends SQLiteOpenHelper

{

    public MyDataBase(Context context, String name, CursorFactory factory, int version)

    {

        super(context, name, factory, version);

        // TODO Auto-generated constructor stub

    }


    public void onCreate(SQLiteDatabase db)

    {

        // TODO Auto-generated method stub

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

    }


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

    {

        // TODO Auto-generated method stub


    }

}


2.activity_image_retrive.xml:

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

<androidx.constraintlayout.widget.ConstraintLayout 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=".image.retrive.Image_retrive">

    <ImageView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:id="@+id/imageView"></ImageView>


</androidx.constraintlayout.widget.ConstraintLayout>


3.AndroidManifest.xml:

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

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

    package="com.akash.myapplication">

<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=".image.retrive.Image_retrive">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />


                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>


</manifest>



4.styles.xml:

<resources>

    <!-- Base application theme. -->

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

        <!-- Customize your theme here. -->

        <item name="colorPrimary">@color/colorPrimary</item>

        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>

        <item name="colorAccent">@color/colorAccent</item>

    </style>


</resources>


5.colors.xml:

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

<resources>

    <color name="colorPrimary">#6200EE</color>

    <color name="colorPrimaryDark">#3700B3</color>

    <color name="colorAccent">#03DAC5</color>

</resources>


6.strings.xml:

<resources>

    <string name="app_name">retrieve image from sqlite database in android </string>

</resources>


7.Output:


retrieve image from sqlite database in android



No comments:

Post a Comment