Advertisement
mythoc

Untitled

Jun 19th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. /private DocumentReference UsersRef;
  2. /UsersRef = FirebaseFirestore.getInstance().collection("Users").document(currentUserID);
  3.  
  4. UsersRef.addValueEventListener(new ValueEventListener() {
  5.             @Override
  6.             public void onDataChange(DataSnapshot dataSnapshot)
  7.             {
  8.                 if(dataSnapshot.exists())
  9.                 {
  10.                     if (dataSnapshot.hasChild("profileimage"))
  11.                     {
  12.                         String image = dataSnapshot.child("profileimage").getValue().toString();
  13.                         Picasso.with(SetupActivity.this).load(image).placeholder(R.drawable.profile).into(ProfileImage);
  14.                     }
  15.                     else
  16.                     {
  17.                         Toast.makeText(SetupActivity.this, "Please select profile image first.", Toast.LENGTH_SHORT).show();
  18.                     }
  19.                 }
  20.             }
  21.  
  22.             @Override
  23.             public void onCancelled(DatabaseError databaseError) {
  24.  
  25.             }
  26.         });
  27.     }
  28.  
  29.  
  30.  
  31.     @Override
  32.     protected void onActivityResult(int requestCode, int resultCode, Intent data)
  33.     {
  34.         super.onActivityResult(requestCode, resultCode, data);
  35.  
  36.         if(requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null)
  37.         {
  38.             Uri ImageUri = data.getData();
  39.  
  40.             CropImage.activity()
  41.                     .setGuidelines(CropImageView.Guidelines.ON)
  42.                     .setAspectRatio(1, 1)
  43.                     .start(this);
  44.         }
  45.  
  46.         if(requestCode==CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
  47.         {
  48.             CropImage.ActivityResult result = CropImage.getActivityResult(data);
  49.  
  50.             if(resultCode == RESULT_OK)
  51.             {
  52.                 loadingBar.setTitle("Profile Image");
  53.                 loadingBar.setMessage("Please wait, while we updating your profile image...");
  54.                 loadingBar.show();
  55.                 loadingBar.setCanceledOnTouchOutside(true);
  56.  
  57.                 Uri resultUri = result.getUri();
  58.  
  59.                 StorageReference filePath = UserProfileImageRef.child(currentUserID + ".jpg");
  60.  
  61.                 filePath.putFile(resultUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
  62.                     @Override
  63.                     public void onComplete(@NonNull final Task<UploadTask.TaskSnapshot> task)
  64.                     {
  65.                         if(task.isSuccessful())
  66.                         {
  67.                             Toast.makeText(SetupActivity.this, "Profile Image stored successfully to Firebase storage...", Toast.LENGTH_SHORT).show();
  68.  
  69.                             final String downloadUrl = task.getResult().getDownloadUrl().toString();
  70.  
  71.                             UsersRef.child("profileimage").setValue(downloadUrl)
  72.                                     .addOnCompleteListener(new OnCompleteListener<Void>() {
  73.                                         @Override
  74.                                         public void onComplete(@NonNull Task<Void> task)
  75.                                         {
  76.                                             if(task.isSuccessful())
  77.                                             {
  78.                                                 Intent selfIntent = new Intent(SetupActivity.this, SetupActivity.class);
  79.                                                 startActivity(selfIntent);
  80.  
  81.                                                 Toast.makeText(SetupActivity.this, "Profile Image stored to Firebase Database Successfully...", Toast.LENGTH_SHORT).show();
  82.                                                 loadingBar.dismiss();
  83.                                             }
  84.                                             else
  85.                                             {
  86.                                                 String message = task.getException().getMessage();
  87.                                                 Toast.makeText(SetupActivity.this, "Error Occured: " + message, Toast.LENGTH_SHORT).show();
  88.                                                 loadingBar.dismiss();
  89.                                             }
  90.                                         }
  91.                                     });
  92.                         }
  93.                     }
  94.                 });
  95.             }
  96.             else
  97.             {
  98.                 Toast.makeText(this, "Error Occured: Image can not be cropped. Try Again.", Toast.LENGTH_SHORT).show();
  99.                 loadingBar.dismiss();
  100.             }
  101.         }
  102.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement