daily pastebin goal
20%
SHARE
TWEET

Profile

DP13 Jan 16th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package dp13.com.firebasemessenger.Fragments;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.ContentResolver;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.Bundle;
  8. import android.support.annotation.NonNull;
  9. import android.support.v4.app.Fragment;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.webkit.MimeTypeMap;
  14. import android.widget.TextView;
  15. import android.widget.Toast;
  16.  
  17. import com.bumptech.glide.Glide;
  18. import com.google.android.gms.tasks.Continuation;
  19. import com.google.android.gms.tasks.OnCompleteListener;
  20. import com.google.android.gms.tasks.OnFailureListener;
  21. import com.google.android.gms.tasks.Task;
  22. import com.google.firebase.auth.FirebaseAuth;
  23. import com.google.firebase.auth.FirebaseUser;
  24. import com.google.firebase.database.DataSnapshot;
  25. import com.google.firebase.database.DatabaseError;
  26. import com.google.firebase.database.DatabaseReference;
  27. import com.google.firebase.database.FirebaseDatabase;
  28. import com.google.firebase.database.ValueEventListener;
  29. import com.google.firebase.storage.FirebaseStorage;
  30. import com.google.firebase.storage.StorageReference;
  31. import com.google.firebase.storage.StorageTask;
  32. import com.google.firebase.storage.UploadTask;
  33. import com.mikhaellopez.circularimageview.CircularImageView;
  34.  
  35. import java.util.HashMap;
  36.  
  37. import dp13.com.firebasemessenger.Object.User;
  38. import dp13.com.firebasemessenger.R;
  39.  
  40. import static android.app.Activity.RESULT_OK;
  41.  
  42. public class ProfileFragment extends Fragment{
  43.  
  44.     private static final int IMG_REQUEST = 1;
  45.     private Uri imgUri;
  46.     private StorageTask uploadTask;
  47.     CircularImageView frag_profile_image;
  48.     TextView username;
  49.  
  50.     FirebaseUser firebaseUser;
  51.     DatabaseReference reference;
  52.  
  53.     StorageReference storageReference;
  54.  
  55.     @Override
  56.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  57.         View view = inflater.inflate(R.layout.fragment_profile, container, false);
  58.  
  59.         frag_profile_image = view.findViewById(R.id.frag_profile_image);
  60.         username = view.findViewById(R.id.username);
  61.  
  62.         firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  63.         reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
  64.         storageReference = FirebaseStorage.getInstance().getReference("uploads");
  65.  
  66.         reference.addValueEventListener(new ValueEventListener() {
  67.             @Override
  68.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  69.                 User user = dataSnapshot.getValue(User.class);
  70.                 username.setText(user.getUsername());
  71.                 if (user.getImageURL().equals("default")){
  72.                     frag_profile_image.setImageResource(R.drawable.duc);
  73.                 } else {
  74.                     Glide.with(getContext()).load(user.getImageURL()).into(frag_profile_image);
  75.                 }
  76.             }
  77.  
  78.             @Override
  79.             public void onCancelled(@NonNull DatabaseError databaseError) {
  80.  
  81.             }
  82.         });
  83.  
  84.         frag_profile_image.setOnClickListener(new View.OnClickListener(){
  85.             @Override
  86.             public void onClick(View view){
  87.                 openImage();
  88.             }
  89.         });
  90.  
  91.         return view;
  92.     }
  93.  
  94.     private void openImage(){
  95.         Intent intent = new Intent();
  96.         intent.setType("image/*");
  97.         intent.setAction(Intent.ACTION_GET_CONTENT);
  98.         startActivityForResult(intent, IMG_REQUEST);
  99.     }
  100.  
  101.     private String getFileExtension(Uri uri){
  102.         ContentResolver contentResolver = getContext().getContentResolver();
  103.         MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
  104.         return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
  105.     }
  106.  
  107.     private void uploadImage(){
  108.         final ProgressDialog progressDialog = new ProgressDialog(getContext());
  109.         progressDialog.setMessage("Uploading...");
  110.         progressDialog.show();
  111.  
  112.         if(imgUri != null){
  113.             final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
  114.             +"."+ getFileExtension(imgUri));
  115.  
  116.             uploadTask = fileReference.putFile(imgUri);
  117.             uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot,Task<Uri>>() {
  118.                 @Override
  119.                 public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
  120.                     if(!task.isSuccessful()){
  121.                         throw task.getException();
  122.                     }
  123.                     return fileReference.getDownloadUrl();
  124.                 }
  125.             }).addOnCompleteListener(new OnCompleteListener<Uri>() {
  126.                 @Override
  127.                 public void onComplete(@NonNull Task<Uri> task) {
  128.                     if(task.isSuccessful()){
  129.                         Uri downloadUri = task.getResult();
  130.                         String mUri = downloadUri.toString();
  131.  
  132.                         reference = FirebaseDatabase.getInstance().getReference("Users").child(firebaseUser.getUid());
  133.                         HashMap<String, Object> hashMap = new HashMap<>();
  134.                         hashMap.put("imageURL", mUri);
  135.                         reference.updateChildren(hashMap);
  136.  
  137.                         progressDialog.dismiss();
  138.                     }else {
  139.                         Toast.makeText(getContext(), "Upload failed", Toast.LENGTH_SHORT).show();
  140.                         progressDialog.dismiss();
  141.                     }
  142.                 }
  143.             }).addOnFailureListener(new OnFailureListener() {
  144.                 @Override
  145.                 public void onFailure(@NonNull Exception e) {
  146.                     Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
  147.                     progressDialog.dismiss();
  148.                 }
  149.             });
  150.         } else {
  151.             Toast.makeText(getContext(), "No image selected", Toast.LENGTH_SHORT).show();
  152.         }
  153.     }
  154.  
  155.     @Override
  156.     public void onActivityResult(int requestCode, int resultCode, Intent data) {
  157.         super.onActivityResult(requestCode, resultCode, data);
  158.  
  159.         if (requestCode == IMG_REQUEST && resultCode == RESULT_OK &&
  160.                 data != null && data.getData() != null ){
  161.             imgUri = data.getData();
  162.  
  163.             if(uploadTask != null && uploadTask.isInProgress()){
  164.                 Toast.makeText(getContext(), "Upload in progress...", Toast.LENGTH_SHORT).show();
  165.             }else {
  166.                 uploadImage();
  167.             }
  168.         }
  169.     }
  170. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top