Jai Saraswati Maa"
Hi .... Dear All .... Today I am uploading a superb example of Image with SQLite Database
In this example you can capture image from Camera and store image in your specified folder and path in database as well as you can retrieve path from database and image from folder.
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
Special Thanks Dilip Singh
@@@@ please add read write permission in manifest file like
Hi .... Dear All .... Today I am uploading a superb example of Image with SQLite Database
In this example you can capture image from Camera and store image in your specified folder and path in database as well as you can retrieve path from database and image from folder.
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
Special Thanks Dilip Singh
@@@@ 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. Output will be like thisB. XML file activity_main.xml is as<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="com.example.student.imagepathsqlite.MainActivity"> <Button android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Capture Image"android:id="@+id/btnSelect"android:onClick="clickImage"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="Save Image"android:id="@+id/btnUpload"android:onClick="sand"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="Show Image"android:id="@+id/button"android:onClick="show"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/imageView1"android:src="@mipmap/ic_launcher"android:layout_alignParentBottom="true"android:layout_alignStart="@+id/imageView"android:layout_marginTop="300dp" /> </RelativeLayout>C. Java Files are1. MainActivity.javapackage com.example.student.imagepathsqlite; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.net.Uri; import android.provider.MediaStore; 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.File; import java.util.Calendar; public class MainActivity extends AppCompatActivity { ImageView imgv1,imgv2; Bitmap bm =null; int REQUEST_CHECK = 0; String imgs; String s; File imgpath; private Cursor c=null; private MyDataBase mdb=null; private SQLiteDatabase db=null; private static final String DB_NAME = "ImgDb.db"; public static final int DB_VERSION = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imgv1 = (ImageView) findViewById(R.id.imageView); imgv2 = (ImageView) findViewById(R.id.imageView1); mdb=new MyDataBase(getApplicationContext(), DB_NAME,null, DB_VERSION); } public void clickImage(View view) { //fire intentIntent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File fp = getFile(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(fp)); startActivityForResult(intent, REQUEST_CHECK); } private File getFile() { File folder = new File("sdcard/attendence"); if (!folder.exists()) { folder.mkdir(); } imgpath = new File(folder, File.separator +Calendar.getInstance().getTime() + ".jpg"); return imgpath; } @Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { try{ s = imgpath.toString(); Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); } catch (Exception e){ e.printStackTrace(); } imgv1.setImageDrawable(Drawable.createFromPath(s)); } public void sand(View view) { db=mdb.getWritableDatabase(); ContentValues cv =new ContentValues(); cv.put("path" ,s); db.insert("tableimage", null, cv); Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show(); } public void show (View view) { String col[] = {"path"}; db = mdb.getReadableDatabase(); c = db.query("tableimage", col, null, null, null, null, null); c.moveToLast(); imgs = c.getString(c.getColumnIndex("path")); imgv2.setImageDrawable(Drawable.createFromPath(imgs)); } }2. for Handling Database MyDataBase.java is as
package com.example.student.imagepathsqlite;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 (path varchar(255))"); } @Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }