Guest User

Untitled

a guest
Jan 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. public class RegisterPhotoActivity extends Activity {
  2.  
  3. Button uploadProfilePhoto;
  4. ImageView checkmarkImage, backArrowImage;
  5.  
  6. private FirebaseAuth auth;
  7. private static FirebaseUser currentUser;
  8. String currentUserString;
  9.  
  10. private static final int SELECTED_PICTURE = 1;
  11.  
  12. FirebaseStorage storage;
  13. StorageReference storageReference;
  14.  
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_registerphoto);
  19.  
  20. uploadProfilePhoto = (Button) findViewById(R.id.uploadPhotoBTN);
  21. checkmarkImage = (ImageView) findViewById(R.id.checkmarkImage);
  22.  
  23. backArrowImage = (ImageView) findViewById(R.id.leftArrow);
  24. backArrowImage.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View view) {
  27. Intent intent = new Intent(RegisterPhotoActivity.this, RegisterActivity.class);
  28. startActivity(intent);
  29. }
  30. });
  31.  
  32. auth = FirebaseAuth.getInstance();
  33.  
  34. currentUser =
  35. auth.getCurrentUser();
  36. currentUserString = currentUser.toString();
  37. Log.i("CurrentUserString", currentUserString);
  38.  
  39. storage = FirebaseStorage.getInstance();
  40. //storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com").child("20170702_174811.jpeg");
  41. //storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com").child(currentUserString);
  42. storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com/images").child(currentUserString);
  43.  
  44.  
  45.  
  46. checkmarkImage.setOnClickListener(new View.OnClickListener() {
  47.  
  48. @Override
  49. public void onClick(View v) {
  50. Intent intent = new Intent(RegisterPhotoActivity.this, RegisterBusinessActivity.class);
  51. startActivity(intent);
  52. }
  53. });
  54.  
  55. uploadProfilePhoto.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View view) {
  58. handleChooseImage(view);
  59. }
  60. });
  61.  
  62. }
  63.  
  64. //END OF onCreate
  65.  
  66. //Separate methods
  67.  
  68. //Actually opens the CameraRoll
  69. public void handleChooseImage(View v) {
  70. Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  71. startActivityForResult(i, SELECTED_PICTURE); //then goes to onActivityResult
  72. }
  73. public void handleInsertData(View v) {
  74.  
  75. }
  76.  
  77. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  78. super.onActivityResult(requestCode, resultCode, data);
  79. switch (requestCode) {
  80. case 0:
  81. if(requestCode == RESULT_OK) {
  82. Log.i("RegisterActivity", "case 0");
  83. }
  84. break;
  85. case 1:
  86. if(resultCode == RESULT_OK && data != null) {
  87.  
  88. Uri selectedImage = data.getData();
  89. Log.i("RegisterActivity", "selected image = " + selectedImage);
  90. Bitmap imageBitmap = null;
  91. try {
  92. imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
  93. } catch (IOException e) {
  94. e.printStackTrace();
  95. }
  96. encodeBitmapAndSaveToFirebase(imageBitmap);
  97. }
  98. break;
  99. }
  100. }
  101.  
  102. public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
  103. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  104. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //was PNG
  105. byte[] data = baos.toByteArray();
  106.  
  107. UploadTask uploadTask = storageReference.putBytes(data);
  108. uploadTask.addOnFailureListener(new OnFailureListener() {
  109. @Override
  110. public void onFailure(@NonNull Exception exception) {
  111. }
  112. }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
  113. @Override
  114. public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
  115. Toast.makeText(RegisterPhotoActivity.this, "reached onSuccess:", Toast.LENGTH_SHORT).show();
  116.  
  117. }
  118. });
  119. }
  120. }
  121.  
  122. service firebase.storage {
  123. match /b/{bucket}/o {
  124. match /{allPaths=**} {
  125. allow read, write: if request.auth != null;
  126. }
  127. match /images {
  128. // Only an individual user can write to "their" images
  129. match /{userId}/{imageId} {
  130. allow write: if request.auth.uid == userId;
  131. }
  132. }
  133.  
  134. }
  135.  
  136. }
Add Comment
Please, Sign In to add comment