Guest User

Untitled

a guest
Jan 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  2. super.onActivityResult(requestCode, resultCode, data);
  3. switch (requestCode) {
  4. case 0:
  5. if(requestCode == RESULT_OK) {
  6. Log.i("RegisterActivity", "case 0");
  7. }
  8. break;
  9. case 1:
  10. if(resultCode == RESULT_OK && data != null) {
  11.  
  12. Uri selectedImage = data.getData();
  13. Log.i("RegisterActivity", "selected image = " + selectedImage);
  14. //Bitmap imageBitmap = null;
  15. try {
  16. imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage); //The Image/Photo
  17. // https://stackoverflow.com/questions/24629584/image-orientation-changes-while-uploading-image-to-server
  18. //File pictureFile = (File) imageBitmap;
  19. ExifInterface exif= new ExifInterface();
  20.  
  21. //exif = new ExifInterface();
  22. int angle = 0;
  23. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  24. if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
  25. angle = 90;
  26. }
  27. else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
  28. angle = 180;
  29. }
  30. Matrix matrix1 = new Matrix();
  31. //set image rotation value to 45 degrees in matrix.
  32. matrix1.postRotate(angle);
  33. //Create bitmap with new values.
  34. Bitmap photo = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap.getHeight(), matrix1, true);
  35. profilePhoto.setImageBitmap(imageBitmap); //Placing image in ImageView
  36.  
  37. public class EditProfileActivity extends Activity {
  38.  
  39. private EditText firstNameET, lastNameET, passwordET, confirmPasswordET, businessTitleET;
  40. private String firstName, lastName, password, confirmPassword, businessTitle, uid,currentUserString;
  41. private ImageView profilePhoto, xImage, checkmarkImage;
  42. private Button changePhoto;
  43. private DatabaseReference employeesRef;
  44. private FirebaseUser currentUser;
  45. private FirebaseAuth auth;
  46. private AuthCredential credential;
  47. FirebaseStorage storage;
  48. StorageReference storageReference;
  49. private static final int SELECTED_PICTURE = 1;
  50. //Test variable
  51. Uri downloadUrl;
  52. Bitmap imageBitmap;
  53.  
  54.  
  55. @Override
  56. protected void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.activity_edit_profile);
  59.  
  60. //INITIALIZE VARIABLES
  61. initVariables();
  62.  
  63.  
  64. //UPDATE PHOTO
  65. changePhoto.setOnClickListener(new View.OnClickListener() {
  66. @Override
  67. public void onClick(View view) {
  68. Log.i("photoBTN","InPhotoBTN");
  69. handleChooseImage(view);
  70. }
  71. });
  72.  
  73.  
  74. //UPDATE DATA FIELDS
  75. checkmarkImage.setOnClickListener(new View.OnClickListener() {
  76. @Override
  77. public void onClick(View view) {
  78.  
  79. Log.i("CHECK","INSIDE CHECKMARK");
  80. updateDBFields();
  81.  
  82. } //END ONCLICK
  83. }); //END ONCLICKLISTENER
  84.  
  85.  
  86.  
  87. }
  88.  
  89. void initVariables(){
  90. Log.i("initVariables","inside method");
  91. firstNameET = (EditText) findViewById(R.id.firstNameET);
  92. lastNameET = (EditText) findViewById(R.id.lastNameET);
  93. passwordET = (EditText) findViewById(R.id.changePasswordET);
  94. confirmPasswordET = (EditText) findViewById(R.id.passwordConfirmET);
  95.  
  96.  
  97. profilePhoto = (ImageView) findViewById(R.id.profilePhoto);
  98. changePhoto = (Button) findViewById(R.id.changePhotoBTN);
  99. xImage = (ImageView) findViewById(R.id.xImage);
  100. checkmarkImage = (ImageView) findViewById(R.id.checkmarkImage);
  101. employeesRef = FirebaseDatabase.getInstance().getReference("Employees");
  102.  
  103. currentUser = FirebaseAuth.getInstance().getCurrentUser();
  104. currentUserString = currentUser.toString();
  105. Log.i("CurrentUserString", currentUserString);
  106. storage = FirebaseStorage.getInstance();
  107. storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com/images").child(currentUserString);
  108.  
  109. auth = FirebaseAuth.getInstance();
  110. uid = currentUser.getUid();
  111. }
  112.  
  113. void updateDBFields() {
  114.  
  115. firstName = firstNameET.getText().toString().trim();
  116. lastName = lastNameET.getText().toString().trim();
  117. password = passwordET.getText().toString().trim();
  118. confirmPassword = confirmPasswordET.getText().toString().trim();
  119.  
  120.  
  121. //UPDATE NAME
  122. if(!TextUtils.isEmpty(firstName)) {
  123. Log.i("UID", uid);
  124. employeesRef.child(uid).child("firstName").setValue(firstName);
  125. }
  126.  
  127. if(!TextUtils.isEmpty(lastName)) {
  128. Log.i("UID", uid);
  129. employeesRef.child(uid).child("lastName").setValue(lastName);
  130. }
  131.  
  132. //UPDATE PASSWORD
  133. if ( !(TextUtils.isEmpty(password)) && !(TextUtils.isEmpty(confirmPassword)) ) {
  134. if(password.equals(confirmPassword)) {
  135. currentUser.updatePassword(password);
  136. return;
  137. }
  138. }
  139. }
  140.  
  141. public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
  142. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  143. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //was PNG
  144. byte[] data = baos.toByteArray();
  145. UploadTask uploadTask = storageReference.putBytes(data);
  146. uploadTask.addOnFailureListener(new OnFailureListener() {
  147. @Override
  148. public void onFailure(@NonNull Exception exception) {
  149. Log.i("OnFailure", exception.toString());
  150. }
  151. }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  152. @Override
  153. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  154. Toast.makeText(EditProfileActivity.this, "reached onSuccess:", Toast.LENGTH_SHORT).show();
  155. Log.i("Success", "Reached Success");
  156.  
  157. //Get the URL of the Image
  158. downloadUrl = taskSnapshot.getDownloadUrl();
  159. //Uri downloadUrl = taskSnapshot.getDownloadUrl();
  160.  
  161. //Use a Map for Key/Value pair
  162. Map<String, Object> map = new HashMap<>();
  163. map.put("photoDownloadUrl", downloadUrl.toString());
  164.  
  165. //Add the URL in the Map to the Firebase DB
  166. employeesRef.child(currentUser.getUid()).updateChildren(map);
  167. }
  168. });
  169. }
  170.  
  171. //Actually opens the CameraRoll
  172. public void handleChooseImage(View v) {
  173.  
  174. Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  175. startActivityForResult(i, SELECTED_PICTURE); //then goes to onActivityResult
  176. }
  177. public void handleInsertData(View v) {
  178.  
  179. }
  180.  
  181. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  182. super.onActivityResult(requestCode, resultCode, data);
  183. switch (requestCode) {
  184. case 0:
  185. if(requestCode == RESULT_OK) {
  186. Log.i("RegisterActivity", "case 0");
  187. }
  188. break;
  189. case 1:
  190. if(resultCode == RESULT_OK && data != null) {
  191.  
  192. Uri selectedImage = data.getData();
  193. Log.i("RegisterActivity", "selected image = " + selectedImage);
  194. //Bitmap imageBitmap = null;
  195. try {
  196. imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage); //The Image/Photo
  197. // https://stackoverflow.com/questions/24629584/image-orientation-changes-while-uploading-image-to-server
  198. //File pictureFile = (File) imageBitmap;
  199. ExifInterface exif= new ExifInterface();
  200.  
  201. //exif = new ExifInterface();
  202. int angle = 0;
  203. int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
  204. if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
  205. angle = 90;
  206. }
  207. else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
  208. angle = 180;
  209. }
  210. Matrix matrix1 = new Matrix();
  211. //set image rotation value to 45 degrees in matrix.
  212. matrix1.postRotate(angle);
  213. //Create bitmap with new values.
  214. Bitmap photo = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap.getHeight(), matrix1, true);
  215. profilePhoto.setImageBitmap(imageBitmap); //Placing image in ImageView
  216. }
  217. catch (IOException e) {
  218. e.printStackTrace();
  219. }
  220. encodeBitmapAndSaveToFirebase(imageBitmap);
  221. }
  222. break;
  223. }
  224. }
  225. }
Add Comment
Please, Sign In to add comment