Tutorial 9 - Update Image

 


This is Sample Project Which you can improve UI design & Coding skills .

*******Important********
1)You need add this dependencies to build.gradle. 
implementation 'com.squareup.picasso:picasso:2.71828'
implementation 'de.hdodenhof:circleimageview:3.0.1'

If you get any trouble. Please Contact me on WhatsApp!!

Download link for Profile Activity Source code



ProfileActivity

activity_profile

package com.rrmchathura.myfirstapp;

import android.Manifest;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.StorageTask;
import com.google.firebase.storage.UploadTask;
import com.squareup.picasso.Picasso;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;





public class ProfileActivity extends AppCompatActivity {

private static final String TAG = "ProfileActivity" ;
private EditText fullName,email,phone;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
Button resetPassLocal,changeProfileImage;
FirebaseUser user;
ImageView profileImage;
private ProgressDialog mProgressDialog;

private CircleImageView profileImageView;
private Button closeButton,saveButton;
private TextView profileChangeBtn;

private DatabaseReference databaseReference;
private Uri imageUri;
private String myUri = "";
private StorageTask uploadTask;
private StorageReference storageReference;
int TAKE_IMAGE_CODE = 10001;
ProgressBar progressBar;


Bitmap original_image;


private static final int PReqCode = 2 ;
private static final int REQ_CODE = 1000;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);



fullName = findViewById(R.id.profileName);
email = findViewById(R.id.profileEmail);
phone = findViewById(R.id.profilePhone);
resetPassLocal = findViewById(R.id.resetPasswordLocal);
profileImage = findViewById(R.id.profile_image);
changeProfileImage = findViewById(R.id.changeProfile);
progressBar = findViewById(R.id.progressBar);

mProgressDialog = new ProgressDialog(this);


fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();

StorageReference profileRef = storageReference.child("users/"+
FirebaseAuth.getInstance().getCurrentUser().getUid()+"/profile.jpg");
profileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Picasso.get().load(uri).into(profileImage);
}
});

userID = fAuth.getCurrentUser().getUid();
user = fAuth.getCurrentUser();

DocumentReference documentReference = fStore.
collection("users").document(userID);
documentReference.
addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot,
@Nullable FirebaseFirestoreException error){
phone.setText(documentSnapshot.getString("phone"));
fullName.setText(documentSnapshot.getString("fName"));
email.setText(documentSnapshot.getString("email"));
}
});
//////////////Reset Password Button Clicked

resetPassLocal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText resetPassword = new EditText(v.getContext());
AlertDialog.Builder passwordResetDialog = new
AlertDialog.Builder(v.getContext());

passwordResetDialog.setTitle("Reset Password ?");
passwordResetDialog.setMessage("Enter New Password > 6 Characters long.");

passwordResetDialog.setView(resetPassword);

passwordResetDialog.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//extract the email and send reset link

String newPassword = resetPassword.getText().toString();
user.updatePassword(newPassword).
addOnSuccessListener(new OnSuccessListener<Void>()
{
@Override
public void onSuccess(Void unused) {
Toast.makeText(ProfileActivity.this,
"Password Reset Successfully",
Toast.LENGTH_SHORT)
.show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ProfileActivity.
this,"Password Reset Failed",
Toast.LENGTH_SHORT)
.show();
}
});

}
});
passwordResetDialog.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//close the dialog box
}
});
passwordResetDialog.create().show();

}
});

changeProfileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
checkAndRequestForPermission();
}
});

}

private void checkAndRequestForPermission() {
if (ContextCompat.checkSelfPermission(ProfileActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(ProfileActivity.
this, Manifest.permission.READ_EXTERNAL_STORAGE)) {

Toast.makeText(ProfileActivity.this,
"Please accept for required permission",
Toast.LENGTH_SHORT).show();
}
else
{
ActivityCompat.requestPermissions(ProfileActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PReqCode);
}
return;
}else{
Intent OpenGalleryIntent= new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(OpenGalleryIntent,REQ_CODE);
}
}

@Override
public void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000) {
if (resultCode == Activity.RESULT_OK) {


imageUri = data.getData();
try {

UploadToDatabase(imageUri);

} catch (Exception e) {
Log.e("TAG", "Error : " + e);
}

}

}

}

///////////////////////upload image to Firebase/////////////////////////////
private void UploadToDatabase(Uri imageUri) {
progressBar.setVisibility(View.VISIBLE);
StorageReference fileRef = storageReference.child("users/"+ FirebaseAuth.
getInstance().getCurrentUser().getUid()+"/profile.jpg");
fileRef.putFile(imageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.
TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
fileRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
{
@Override
public void onSuccess(Uri uri) {
Toast.makeText(ProfileActivity.this,
"Profile Uploading Sucess ....",Toast.LENGTH_SHORT)
.show();
progressBar.setVisibility(View.GONE);
Picasso.get().load(uri).into(profileImage);
}
});
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(ProfileActivity.this,
"Profile Uploading Failed ....",Toast.LENGTH_SHORT)
.show();
}
});
}

public void dialogBox(String message,String title) {

AlertDialog alertDialog = new AlertDialog.Builder(ProfileActivity.
this).create();
alertDialog.setTitle(title);
alertDialog.setMessage(message);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});

alertDialog.show();
}
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileActivity"
android:orientation="vertical">


<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/top_gradient" />

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Covid Tracker"
android:textStyle="bold"
android:layout_marginTop="-40dp"
android:height="30dp"
android:fontFamily="sans-serif-smallcaps"
android:textAlignment="center"
android:textSize="25dp"
android:textColor="@color/white"
android:gravity="center_horizontal" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="30dp"
>



<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp"
android:orientation="vertical"
android:background="@drawable/bg_top">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginTop="20dp"
android:layout_gravity="center"
app:civ_border_color="@color/white"
app:civ_border_width="5dp"
android:scaleType="centerCrop"
android:src="@drawable/user"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome To Profile"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:textAlignment="center"
android:textAllCaps="true"
android:fontFamily="@font/baloo"
android:textSize="20dp"
/>

</LinearLayout>
<Button
android:id="@+id/changeProfile"
android:layout_width="150dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_marginTop="-19dp"
android:background="@drawable/button_style"
android:fontFamily="@font/baloo"
android:text="Chage Profile"
android:textColor="@color/white"
android:textSize="15sp"
android:textStyle="bold" />


<ScrollView

android:layout_width="match_parent"
android:layout_height="match_parent"
>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="Username"
android:fontFamily="@font/baloo"
android:textSize="15dp"
/>

<EditText
android:id="@+id/profileName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:padding="10dp"
android:editable="false"
android:textColor="@color/textcolor"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_user"
android:hint="Your Name"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:fontFamily="@font/baloo"

/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginHorizontal="30dp"
android:layout_gravity="center"
android:text="Email"
android:fontFamily="@font/baloo"
android:textSize="15dp"
/>

<EditText
android:id="@+id/profileEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:padding="10dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_email"
android:hint="Your Email Address"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:fontFamily="@font/baloo"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="Mobile"
android:fontFamily="@font/baloo"
android:textSize="15dp"
/>

<EditText
android:id="@+id/profilePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:padding="10dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_phone"
android:hint="Your Phone"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:fontFamily="@font/baloo"
/>

<Button
android:id="@+id/resetPasswordLocal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="30dp"
android:background="@drawable/button_style"
android:text="Reset Password"
android:textColor="@color/white"
android:layout_gravity="center"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:fontFamily="@font/baloo"
android:textSize="18sp"
/>

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="invisible"
android:layout_marginTop="10dp"/>


</LinearLayout>

</ScrollView>
</LinearLayout>




</LinearLayout>


Post a Comment

0 Comments

Youtube Channel Image
Coding With Chathura Subscribe To watch more Project Tutorials
Subscribe
Do you have any doubts? chat with us on WhatsApp
Hello, How can I help you? ...
Click me to start the chat...

Welcome to freesourcecodelk

X