Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. // FireBase Declaration
  2. FirebaseDatabase database;
  3. DatabaseReference myRef;
  4. FirebaseStorage storage;
  5. StorageReference storageRef, imagesRef, imageNameWithPathRef;
  6.  
  7. onCreate(){
  8. // FireBase Initialization
  9. storage = FirebaseStorage.getInstance();
  10. storageRef = storage.getReference();
  11.  
  12. // Create a child reference
  13. // imagesRef now points to "images"
  14. imagesRef = storageRef.child("CustomerImage");
  15.  
  16. // Child references can also take paths
  17. // spaceRef now points to "users/me/profile.png
  18. // imagesRef still points to "images"
  19. imageNameWithPathRef = storageRef.child("CustomerImage/" + getCurrentTimeStamp() + ".jpg");
  20.  
  21. // Reference's path is: "images/space.jpg"
  22. // This is analogous to a file path on disk
  23. imageNameWithPathRef.getPath();
  24.  
  25. // Reference's name is the last segment of the full path: "space.jpg"
  26. // This is analogous to the file name
  27. imageNameWithPathRef.getName();
  28.  
  29. // Reference's bucket is the name of the storage bucket that the files are stored in
  30. imageNameWithPathRef.getBucket();
  31.  
  32. /**
  33. * Get the FireBase DB Object
  34. * Get the reference for the Customer table
  35. * Used Upload task to save the Customer details
  36. */
  37. database = FirebaseDatabase.getInstance();
  38. myRef = database.getReference("Customer");
  39. mProgressDialog.show();
  40. if (null != byteData) {
  41. UploadTask uploadTask = imageNameWithPathRef.putBytes(byteData);
  42. uploadTask.addOnFailureListener(new OnFailureListener() {
  43. @Override
  44. public void onFailure(@NonNull Exception exception) {
  45. // Handle unsuccessful uploads
  46. exception.printStackTrace();
  47. Log.e("Failed to image upload:", String.valueOf(exception) + "");
  48. mProgressDialog.dismiss();
  49. }
  50. }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  51. @Override
  52. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  53. mProgressDialog.dismiss();
  54.  
  55. // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
  56. Uri downloadUrl = taskSnapshot.getDownloadUrl();
  57. Log.e("downloadUrl", downloadUrl.getPath());
  58.  
  59. Customer mCustomer = new Customer();
  60. int id = Integer.parseInt(NewId) + 1;
  61. mCustomer.setId(String.valueOf(id));
  62. mCustomer.setname(name);
  63. mCustomer.setphone(phone);
  64. mCustomer.setaddress(address);
  65. mCustomer.setage(age);
  66. mCustomer.setexperience(experience);
  67. mCustomer.setratings(rating);
  68. mCustomer.setImagePath(imageNameWithPathRef.getPath());
  69. mCustomer.setadded_by_userid(UserId);
  70. mCustomer.setadded_by_username(UserName);
  71. myRef.child(String.valueOf(id)).setValue(mCustomer);
  72. finish();
  73. }
  74. });
  75. } else {
  76. Customer mCustomer = new Customer();
  77. int id = Integer.parseInt(NewId) + 1;
  78. mCustomer.setId(String.valueOf(id));
  79. mCustomer.setname(name);
  80. mCustomer.setphone(phone);
  81. mCustomer.setaddress(address);
  82. mCustomer.setage(age);
  83. mCustomer.setexperience(experience);
  84. mCustomer.setratings(rating);
  85. mCustomer.setImagePath("");
  86. mCustomer.setadded_by_userid(UserId);
  87. mCustomer.setadded_by_username(UserName);
  88. myRef.child(String.valueOf(id)).setValue(mCustomer);
  89. finish();
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement