Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated image storing in database #27

Open
wants to merge 1 commit into
base: avishka
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ 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'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
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'
Expand Down
28 changes: 25 additions & 3 deletions app/src/main/java/com/example/chatapp/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))");

Expand All @@ -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;
Expand Down
30 changes: 21 additions & 9 deletions app/src/main/java/com/example/chatapp/RegisterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ plugins {

task clean(type: Delete) {
delete rootProject.buildDir
}
}