" Jai Saraswati Maa"
Hi .... Dear All .... Today I am uploading a superb example of Image with SQLite Database
In this example you can select image from your Gallery, Can also upload into Data Base and Retrieve from DB.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box
@@@@ please add read write permission in manifest file like
A. Main Layout
Hi .... Dear All .... Today I am uploading a superb example of Image with SQLite Database
In this example you can select image from your Gallery, Can also upload into Data Base and Retrieve from DB.
I am using Android Studio 1.5.1
Minimun SDK API 19
Target SDK API 23
Please Like us & put your valuable suggestions in comment box
@@@@ please add read write permission in manifest file like
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
A. Main Layout
B. 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="com.exam.ravi.sqlimagemyex1.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>
C. MainActivity.java
package com.exam.ravi.sqlimagemyex1; 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.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; 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); } @Overrideprotected 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(); } }D. MyDataBase.java
package com.exam.ravi.sqlimagemyex1; 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);"); } @Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
Thank its very useful to me.....
ReplyDeletethanks once again....sir
How to delete image from DB with a button?
ReplyDeleteExecuse Me sir..
ReplyDeleteIm not intent to view all