Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- downloadUrl = taskSnapshot.getDownloadUrl();
- public class EditProfile extends Fragment{
- private static final int GALLERY_INTENT=2;
- private EditText editProfileName;
- private EditText editProfilePhone;
- private EditText editProfileNick;
- private ImageView image;
- private Uri downloadUrl;
- private Button saveBtn;
- private final int LENGTH = 1000;
- private FirebaseUser user;
- private ProgressDialog progressDialog;
- public EditProfile() {
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- View v = inflater.inflate(R.layout.fragment_edit_profile, container, false);
- image=(ImageView)v.findViewById(R.id.edit_profile_photo);
- user= FirebaseAuth.getInstance().getCurrentUser();
- editProfileName=(EditText)v.findViewById(R.id.edit_profile_name);
- editProfilePhone=(EditText)v.findViewById(R.id.edit_profile_phone);
- saveBtn=(Button)v.findViewById(R.id.edit_profile_save);
- progressDialog = new ProgressDialog(getContext());
- saveBtn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- chageProfile();
- }
- });
- image.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- setImage();
- }
- });
- return v;
- }
- public void setImage(){
- Intent intent = new Intent(Intent.ACTION_PICK);
- intent.setType("image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent,GALLERY_INTENT);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode==GALLERY_INTENT && resultCode==RESULT_OK){
- progressDialog.setTitle("Uploading");
- progressDialog.show();
- Uri uri = data.getData();
- StorageReference storageReference=FirebaseStorage.getInstance().getReference();
- StorageReference file=storageReference.child(user.getUid()+"/photo.jpg");
- file.putFile(uri)
- .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- // Get a URL to the uploaded content
- downloadUrl = taskSnapshot.getDownloadUrl();
- UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
- .setPhotoUri(downloadUrl)
- .build();
- user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
- @Override
- public void onComplete(@NonNull Task<Void> task) {
- progressDialog.dismiss();
- Toast.makeText(getApplicationContext(),"Фото добавлено",Toast.LENGTH_SHORT).show();
- }
- });
- }
- })
- .addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception exception) {
- // Handle unsuccessful uploads
- // ...
- }
- });
- }
- image.setImageURI(user.getPhotoUrl());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement