Skip to content

Instantly share code, notes, and snippets.

@ronnieotieno
Created July 19, 2019 07:57
Show Gist options
  • Save ronnieotieno/c2f59c25161365326713e99dc6df40aa to your computer and use it in GitHub Desktop.
Save ronnieotieno/c2f59c25161365326713e99dc6df40aa to your computer and use it in GitHub Desktop.
public class ProfileActivity extends Fragment {
private static final int CHOOSE_IMAGE = 101;
Context context;
ImageView imageView;
EditText editTextName, editTextEmail, editTextPhone, editTextAddress, editTextCity;
Button update;
private static final String KEY_EMAIL = "email";
private static final String KEY_NUMBER = "number";
private static final String KEY_ADDRESS = "address";
private static final String KEY_CITY = "city";
Uri uriProfileImage;
ProgressBar progressBar;
String profileImageUrl;
FirebaseAuth mAuth;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View navigationView = inflater.inflate(R.layout.activity_profile, container, false);
editTextName = navigationView.findViewById(R.id.editName);
editTextEmail = navigationView.findViewById(R.id.editEmail);
editTextPhone = navigationView.findViewById(R.id.editPhone);
editTextAddress = navigationView.findViewById(R.id.editAddress);
editTextCity = navigationView.findViewById(R.id.editCity);
imageView = navigationView.findViewById(R.id.imageView);
progressBar = navigationView.findViewById(R.id.progressbar);
update = navigationView.findViewById(R.id.btnSave);
ImageView imageEditpen = navigationView.findViewById(R.id.editPen);
imageEditpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showImageChooser();
}
});
loadUserInformation();
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveUserInformation();
}
});
return navigationView;
}
private void showImageChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Profile Image"), CHOOSE_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CHOOSE_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uriProfileImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uriProfileImage);
imageView.setImageBitmap(bitmap);
uploadImageToFireBaseStorage();
} catch (IOException e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
private void uploadImageToFireBaseStorage() {
StorageReference profileImageRef =
FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");
if (uriProfileImage != null) {
progressBar.setVisibility(View.VISIBLE);
profileImageRef.putFile(uriProfileImage)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressBar.setVisibility(View.GONE);
Toast.makeText(getActivity(), "Profile picture uploaded", Toast.LENGTH_SHORT).show();
Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl();
while (!urlTask.isSuccessful()) ;
Uri downloadUrl = urlTask.getResult();
profileImageUrl = downloadUrl.toString();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressBar.setVisibility(View.GONE);
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment