"Jai Saraswati Maa"
Hi ... Dear All ..Today I am uploading a superb example of Uploading data (Text and Image from Gallery) into DataBase using SQLite.
Please check.
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 Yatendra Balodia (Student Partner)
Please add permissions in manifest file
Hi ... Dear All ..Today I am uploading a superb example of Uploading data (Text and Image from Gallery) into DataBase using SQLite.
Please check.
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 Yatendra Balodia (Student Partner)
Please add permissions in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
A. Layout
B. Layout XML file regact.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"> <TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="First Name" android:id="@+id/tv1"android:layout_alignParentTop="true"android:layout_alignParentLeft="true"android:layout_alignParentStart="true"android:layout_marginTop="30dp" /> <TextView android:layout_width="wrap_content"android:layout_height="wrap_content"android:textAppearance="?android:attr/textAppearanceLarge"android:text="Last Name" android:id="@+id/textView"android:layout_below="@+id/tv1"android:layout_alignRight="@+id/tv1"android:layout_alignEnd="@+id/tv1"android:layout_marginTop="74dp" /> <EditText android:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPersonName" android:ems="10"android:id="@+id/et1" android:layout_alignTop="@+id/tv1"android:layout_alignParentRight="true"android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content"android:layout_height="wrap_content"android:inputType="textPersonName" android:ems="10"android:id="@+id/et2"android:layout_alignBottom="@+id/textView"android:layout_alignRight="@+id/et1"android:layout_alignEnd="@+id/et1" /> <Button android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="SAVE"android:onClick="save" android:id="@+id/bt3"android:layout_marginTop="54dp" android:layout_below="@+id/iv1"android:layout_centerHorizontal="true" /> <Button android:layout_width="100dp"android:layout_height="100dp" android:text="Select Image"android:onClick="selectimg" android:id="@+id/bt4"android:layout_centerVertical="true"android:layout_alignRight="@+id/textView"android:layout_alignEnd="@+id/textView" /> <ImageView android:layout_width="100dp"android:layout_height="100dp" android:id="@+id/iv1"android:background="#dcf7f6" android:layout_alignTop="@+id/bt4"android:layout_alignLeft="@+id/et2"android:layout_alignStart="@+id/et2" /> </RelativeLayout>
C. Java Files
a. Register.java
package yb7.mynewcrud; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; public class Register extends AppCompatActivity { byte img[]; EditText ed1,ed2; ImageView imageView; String str1,str2,str3=null; Bitmap bitmap=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.regact); ed1= (EditText) findViewById(R.id.et1); ed2= (EditText) findViewById(R.id.et2); imageView= (ImageView) findViewById(R.id.iv1); } public void selectimg(View v){ Intent iob = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(iob, 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(); } }}
public void save(View v){ str1=ed1.getText().toString(); str2=ed2.getText().toString(); MyHelper myhlp=new MyHelper(this); myhlp.putInfo(myhlp,str1,str2,img); Toast.makeText(this,"Record Saved",Toast.LENGTH_SHORT).show(); } }
b. MyHelper.java
package yb7.mynewcrud; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyHelper extends SQLiteOpenHelper { private static final String CREATE_QUERY= " create table " + UserInfo.TAB_NAME +" ( " + UserInfo.KEY_ID + " integer primary key autoincrement , "+ UserInfo.F_NAME + " varchar(20), "+ UserInfo.L_NAME + " varchar(20) , " + UserInfo.PIC + " blob ) ;" ; public MyHelper(Context c){ super(c,UserInfo.DB_NAME,null,UserInfo.DB_VER); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_QUERY); } @Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(" drop table if exists " + UserInfo.TAB_NAME); onCreate(db); } public void putInfo(MyHelper mob,String fname,String lname, byte[]img){ SQLiteDatabase sq=mob.getWritableDatabase(); ContentValues cv=new ContentValues(); cv.put(UserInfo.F_NAME,fname); cv.put(UserInfo.L_NAME,lname); cv.put(UserInfo.PIC,img); sq.insert(UserInfo.TAB_NAME,null,cv); } }c. UserInfo.javapackage yb7.mynewcrud; public class UserInfo { public static final int DB_VER=4; public static final String KEY_ID="_id"; public static final String F_NAME="fname"; public static final String L_NAME="lname"; public static final String DB_NAME="mydb2"; public static final String TAB_NAME="usertab"; public static final String PIC="image"; }
No comments:
Post a Comment