Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RegisterPhotoActivity extends Activity {
- Button uploadProfilePhoto;
- ImageView checkmarkImage, backArrowImage;
- private FirebaseAuth auth;
- private static FirebaseUser currentUser;
- String currentUserString;
- private static final int SELECTED_PICTURE = 1;
- FirebaseStorage storage;
- StorageReference storageReference;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_registerphoto);
- uploadProfilePhoto = (Button) findViewById(R.id.uploadPhotoBTN);
- checkmarkImage = (ImageView) findViewById(R.id.checkmarkImage);
- backArrowImage = (ImageView) findViewById(R.id.leftArrow);
- backArrowImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(RegisterPhotoActivity.this, RegisterActivity.class);
- startActivity(intent);
- }
- });
- auth = FirebaseAuth.getInstance();
- currentUser =
- auth.getCurrentUser();
- currentUserString = currentUser.toString();
- Log.i("CurrentUserString", currentUserString);
- storage = FirebaseStorage.getInstance();
- //storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com").child("20170702_174811.jpeg");
- //storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com").child(currentUserString);
- storageReference = storage.getReferenceFromUrl("gs://timeclock-fc.appspot.com/images").child(currentUserString);
- checkmarkImage.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Intent intent = new Intent(RegisterPhotoActivity.this, RegisterBusinessActivity.class);
- startActivity(intent);
- }
- });
- uploadProfilePhoto.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- handleChooseImage(view);
- }
- });
- }
- //END OF onCreate
- //Separate methods
- //Actually opens the CameraRoll
- public void handleChooseImage(View v) {
- Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(i, SELECTED_PICTURE); //then goes to onActivityResult
- }
- public void handleInsertData(View v) {
- }
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- switch (requestCode) {
- case 0:
- if(requestCode == RESULT_OK) {
- Log.i("RegisterActivity", "case 0");
- }
- break;
- case 1:
- if(resultCode == RESULT_OK && data != null) {
- Uri selectedImage = data.getData();
- Log.i("RegisterActivity", "selected image = " + selectedImage);
- Bitmap imageBitmap = null;
- try {
- imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
- } catch (IOException e) {
- e.printStackTrace();
- }
- encodeBitmapAndSaveToFirebase(imageBitmap);
- }
- break;
- }
- }
- public void encodeBitmapAndSaveToFirebase(Bitmap bitmap) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); //was PNG
- byte[] data = baos.toByteArray();
- UploadTask uploadTask = storageReference.putBytes(data);
- uploadTask.addOnFailureListener(new OnFailureListener() {
- @Override
- public void onFailure(@NonNull Exception exception) {
- }
- }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
- @Override
- public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
- Toast.makeText(RegisterPhotoActivity.this, "reached onSuccess:", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- service firebase.storage {
- match /b/{bucket}/o {
- match /{allPaths=**} {
- allow read, write: if request.auth != null;
- }
- match /images {
- // Only an individual user can write to "their" images
- match /{userId}/{imageId} {
- allow write: if request.auth.uid == userId;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment