0

I am trying to upload the image using firebase but I am not able to do so. I am not getting what is going wrong while uploading. Here is the code:

    private void saveUserInformation() {
            mCustomerDatabase.updateChildren(userInfo);
            if (resultUri != null) {
                final StorageReference filepath = FirebaseStorage.getInstance().getReference().child("profileImages").child(userId);
                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), resultUri);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                assert bitmap != null;
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] data = baos.toByteArray();
                UploadTask uploadTask = filepath.putBytes(data);
                uploadTask.addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.d("SA", "Fail1");
                        finish();
                    }
                });
                uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        filepath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri uri) {
                                Map newImage = new HashMap();
                                newImage.put("profileImageUrl", uri.toString());
                                mCustomerDatabase.updateChildren(newImage);
                                Log.d("SA", "Success");
                                finish();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception exception) {
                                Log.e("SA", "Fail2");
                                finish();
                            }
                        });
                    }
                });
             } else {
                   finish();
             }
        }

The StorageException occurs and it is not uploaded in the firebase console as well. Help appreciated!!

Shinnen
  • 3
  • 2
  • please have a look at this https://stackoverflow.com/questions/38670519/addonfilurelistener-being-called-when-i-try-to-upload-images-to-firebase-storage – Asad Ali Jun 03 '20 at 07:55

1 Answers1

0

Define Variable

private static int GALLERY_PICK = 1 ;
private Uri imageUri;
private Button uploudBtn;
private ImageView profileImageView;

imageView, when pressed, chooses the image from the gallery.

profileImageView.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent galleryIntent = new Intent();
                galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent , GALLERY_PICK);
            }
        });

onActivityResult method.

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == GALLERY_PICK && resultCode == RESULT_OK && data != null)
        {
            imageUri = data.getData();
            profileImageView.setImageURI(imageUri);
        }
    }

and then the upload code for image(button )

uploudBtn.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                if(imageUri != null)
                   {
                     final StorageReference filePath = userProfileImageRef.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
            final UploadTask uploadTask = filePath.putFile(imageUri);
            uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>()
            {
                @Override
                public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception
                {
                    if (!task.isSuccessful())
                    {
                        throw  task.getException();
                    }
                    downloadUrl = filePath.getDownloadUrl().toString();
                    return filePath.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>()
            {
                @Override
                public void onComplete(@NonNull Task<Uri> task)
                {
                    if (task.isSuccessful())
                    {
                        downloadUrl = task.getResult().toString();
                      Toast.makeText(MainActivity.this, "Your profile Info has been updated", Toast.LENGTH_SHORT).show();

                   }

            }
        });

**Remark you should cast uploudBtn , profileImageView in OnCreate methods **

profileImageView = findViewById(R.id.imageView);
uploudBtn = findViewById(R.id.button);
Samer Kasseb
  • 192
  • 3
  • 10