From 2db0feb24f1573ce793a42cea4e1ed90b1e32243 Mon Sep 17 00:00:00 2001 From: Avishka Abeywickrama Date: Fri, 3 Jun 2022 20:43:22 +0530 Subject: [PATCH] updated image storing in database --- app/build.gradle | 2 ++ .../java/com/example/chatapp/Database.java | 28 +++++++++++++++-- .../com/example/chatapp/RegisterActivity.java | 30 +++++++++++++------ build.gradle | 3 +- 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 314f34c..8d52980 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,6 +30,7 @@ android { dependencies { + implementation 'com.google.guava:guava:27.0.1-android' implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'com.google.android.material:material:1.5.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' @@ -37,6 +38,7 @@ dependencies { implementation 'com.google.firebase:firebase-auth:19.2.0' implementation 'com.google.firebase:firebase-database:19.2.1' implementation 'com.google.firebase:firebase-storage:19.1.1' + implementation 'androidx.databinding:compiler:3.2.0-alpha11' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' diff --git a/app/src/main/java/com/example/chatapp/Database.java b/app/src/main/java/com/example/chatapp/Database.java index 5d337be..ac0dca7 100644 --- a/app/src/main/java/com/example/chatapp/Database.java +++ b/app/src/main/java/com/example/chatapp/Database.java @@ -8,18 +8,23 @@ import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; +import android.graphics.Bitmap; +import android.graphics.ColorSpace; -import androidx.fragment.app.Fragment; +import androidx.fragment.app.Fragment; +import java.io.ByteArrayOutputStream; import java.util.ArrayList; public class Database extends SQLiteOpenHelper { public static final String DBNAME = "SkyChat.db"; public static int chatID = 0; + private ByteArrayOutputStream objectByteArrayOutputStream; + private byte[] imageInBytes; public Database(Context context) { @@ -34,7 +39,7 @@ public Database() { @Override public void onCreate(SQLiteDatabase skyChatDB) { skyChatDB.execSQL("create Table GROUPCHATS(GROUPNAME TEXT PRIMARY KEY, GROUPADMIN TEXT)"); - skyChatDB.execSQL("create Table USERS(USERNAME TEXT PRIMARY KEY, PASSWORD TEXT)"); + skyChatDB.execSQL("create Table USERS(USERNAME TEXT PRIMARY KEY, PASSWORD TEXT, IMAGE BLOB)"); skyChatDB.execSQL("create Table CHATS(CHATID INT PRIMARY KEY ,USER TEXT , FRIEND TEXT , FRIENDDEVICE BLOB)"); skyChatDB.execSQL("create Table MESSAGES(MSGID INT PRIMARY KEY , CHATID INT ,TYPE TEXT , MSG TEXT , FOREIGN KEY (CHATID) REFERENCES CHATS(CHATID))"); @@ -49,11 +54,28 @@ public void onUpgrade(SQLiteDatabase skyChatDB, int i, int i1) { onCreate(skyChatDB); } - public Boolean insertUSER(String USERNAME,String PASSWORD){ + //public void storeImage(ModelClass object) { + + //Bitmap imageToStoreBitMap = object.getImage(); + + //objectByteArrayOutputStream = new ByteArrayOutputStream(); + //imageToStoreBitMap.compress(Bitmap.CompressFormat.JPEG, 100, objectByteArrayOutputStream); + //imageInBytes = objectByteArrayOutputStream.toByteArray(); + + //} + + public Boolean insertUSER(String USERNAME,String PASSWORD, Bitmap IMAGE){ SQLiteDatabase skyChatDB = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("USERNAME",USERNAME); contentValues.put("PASSWORD",PASSWORD); + + objectByteArrayOutputStream = new ByteArrayOutputStream(); + IMAGE.compress(Bitmap.CompressFormat.JPEG, 100, objectByteArrayOutputStream); + imageInBytes = objectByteArrayOutputStream.toByteArray(); + + contentValues.put("IMAGE",imageInBytes); + long result = skyChatDB.insert("USERS",null,contentValues); if(result==-1) return false; else return true; diff --git a/app/src/main/java/com/example/chatapp/RegisterActivity.java b/app/src/main/java/com/example/chatapp/RegisterActivity.java index d620916..dc13e75 100644 --- a/app/src/main/java/com/example/chatapp/RegisterActivity.java +++ b/app/src/main/java/com/example/chatapp/RegisterActivity.java @@ -4,6 +4,7 @@ import android.app.ProgressDialog; import android.content.Intent; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; @@ -17,11 +18,15 @@ -public class RegisterActivity extends AppCompatActivity { +public class RegisterActivity extends AppCompatActivity { private Button RegisterButton; private EditText UserEmail,UserPassword; private TextView AlreadyHaveAccountLink; + private Uri selectedImage; + private ImageView set_profile_image; + private Bitmap image_to_store; + private ProgressDialog loadingBar; Database skyChatDB; @@ -36,9 +41,9 @@ protected void onCreate(Bundle savedInstanceState) { InitializedFields(); - ImageView gallery = findViewById(R.id.getuserimageinimageview); + set_profile_image = findViewById(R.id.getuserimageinimageview); - gallery.setOnClickListener(new View.OnClickListener() { + set_profile_image.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); @@ -71,14 +76,21 @@ public void onClick(View view) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ - super.onActivityResult(requestCode, resultCode, data); - if(resultCode==RESULT_OK && data!=null) { - Uri selectedImage = data.getData(); - ImageView set_profile_image = findViewById(R.id.getuserimageinimageview); - set_profile_image.setImageURI(selectedImage); + try{ + super.onActivityResult(requestCode, resultCode, data); + if(resultCode==RESULT_OK && data!=null) { + selectedImage = data.getData(); + image_to_store = MediaStore.Images.Media.getBitmap(getContentResolver(),selectedImage); + set_profile_image.setImageBitmap(image_to_store); + } + } + catch (Exception e) { + Toast.makeText(this, "e.getMessage()", Toast.LENGTH_SHORT).show(); } } + + private void createAccount(){ String email1= UserEmail.getText().toString(); String password1 = UserPassword.getText().toString(); @@ -95,7 +107,7 @@ private void createAccount(){ Boolean checkUser = skyChatDB.checkUSERNAME(UserEmail.getText().toString()); if(checkUser==false){ - Boolean insert = skyChatDB.insertUSER(email1,password1); + Boolean insert = skyChatDB.insertUSER(email1,password1,image_to_store); if(insert==true){ Toast.makeText(RegisterActivity.this, "Sucessfull", Toast.LENGTH_SHORT).show(); diff --git a/build.gradle b/build.gradle index f132aaa..0e35089 100644 --- a/build.gradle +++ b/build.gradle @@ -10,4 +10,5 @@ plugins { task clean(type: Delete) { delete rootProject.buildDir -} \ No newline at end of file +} +