Store image into firebase storage with uri

3992 views android
7

I trying to store an image into firebase storage with getDownloadUri but it's already deprecated so I using uri to store image but I have an error in this line: String link = uri.toString; And the error is cannot resolve symbol uri

private DatabaseReference Userdatabase;
private StorageReference storageReference;
private FirebaseAuth firebaseAuth;

private StorageTask mUploadTask;

private Uri imageUri;

ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_signup);

    imageView = (ImageView) findViewById(R.id.imageView);

    firebaseAuth = FirebaseAuth.getInstance();

    Userdatabase = FirebaseDatabase.getInstance().getReference("User");

    progressDialog = new ProgressDialog(Signup.this);

    storageReference = FirebaseStorage.getInstance().getReference("Seller");

    signupButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RegisterAccount();

        }
    });


    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            selectImage();
        }
    });
}

public void selectImage() {
    Intent photoPickerIntent = new Intent();
    photoPickerIntent.setType("image/*");
    photoPickerIntent.setAction(Intent.ACTION_PICK);
    startActivityForResult(photoPickerIntent, PICK_IMAGE_REQUEST);
}

private String getFileExtension(Uri uri) {
    ContentResolver cR = getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(cR.getType(uri));
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {


        imageUri = data.getData();
        Picasso.with(this).load(imageUri).into(imageView);

    }
}


public void RegisterAccount() {

    final String email = emailText.getText().toString();
    final String username = nameText.getText().toString();
    final String pass = passwordText.getText().toString();

    if ((TextUtils.isEmpty(address))) {
        addressText.setError("address is required");
        addressText.requestFocus();
        return;
    }

    AddUser(email, username, pass, phone, comfirmpass, address);


}

public void AddUser(final String UserEmail, final String Username, final String Password,
                    final String PhoneNumber, final String confirmPassword, final String Address) {

    String email = UserEmail.replace(".", ",");

    Userdatabase = FirebaseDatabase.getInstance().getReference("User").child(email);

    Userdatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                String value = dataSnapshot.getValue(String.class);

                Toasty.warning(getApplicationContext(), "The Email you use already Exist !", Toast.LENGTH_SHORT, true).show();
                return;
            }


            if (!dataSnapshot.exists()) {
                if (imageUri != null) {
                  final  StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                            + "." + getFileExtension(imageUri));



                    mUploadTask = fileReference.putFile(imageUri)
                            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                                    fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                                        @Override
                                        public void onSuccess(Uri downloadUri) {
                                            String link = uri.toString;
                                            Toast.makeText(Signup.this, "Register successful", Toast.LENGTH_LONG).show();
                                            final User user = new User(Address, confirmPassword, UserEmail, Password, PhoneNumber, Username,
                                                    link);
                                            Userdatabase.setValue(user);
                                        }
                                    });
                                }
                            });
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            return;
        }
    });

    firebaseAuth.createUserWithEmailAndPassword(UserEmail, Password).addOnCompleteListener(new OnCompleteL

answered question

There's not variable in the function called uri. Maybe you want to use downloadUri.toString()?

1 Answer

4

You are getting the following error:

cannot resolve symbol uri

Because you are calling toString() method on an object that does not exist. To solve this, please change the following line of code:

String link = uri.toString();

to

String link = downloadUri.toString();

posted this

Have an answer?

JD

Please login first before posting an answer.