Conta
Conta

Reputation: 176

FireStorage Java Android Saving the image to the FireStorage requires uploading twice

I am developing a post system with Firestore and to host the image I use Firebase Storage. The user has to fill in 5 fields, and one of them is the image to add. So, in order for the user to select the image, I do this:

     img_imgpost.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // Defining Implicit Intent to mobile gallery
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(
                            Intent.createChooser(
                                    intent,
                                    "Select Image from here..."),
                            PICK_IMAGE_REQUEST);
    
                }
            });
protected void onActivityResult(int requestCode,
                                    int resultCode,
                                    Intent data)
    {



        super.onActivityResult(requestCode,
                resultCode,
                data);


        // checking request code and result code
        // if request code is PICK_IMAGE_REQUEST and
        // resultCode is RESULT_OK
        // then set image in the image view
        if (requestCode == PICK_IMAGE_REQUEST
                && resultCode == RESULT_OK
                && data != null
                && data.getData() != null) {

            // Get the Uri of data
            ImageUri = data.getData();
            try {

                // Setting image on image view using Bitmap
                Bitmap bitmap = MediaStore
                        .Images
                        .Media
                        .getBitmap(
                                getContentResolver(),
                                ImageUri);
                img_imgpost.setImageBitmap(bitmap);
            }

            catch (IOException e) {
                // Log the exception
                e.printStackTrace();
            }
        }
    }

At this point, when the user clicks on the create post button, the app will upload the image and then upload the data entered by the user in the fields. So in the firestore we will have:

         |
         |
         --- title: "Post Title"
         |
         --- date: September 03, 2018 at 6:16:58 PM UTC+3
         |
         --- valutation: 8
         |
         --- urlImage: https// etc


         [...] etc

the problem is that I can't load the image without executing the onclick event of the "create post" button twice, in fact if you press "create post" once, the UrlImage record in the firestore will be null and no image will be loaded on the firestorage.

OnClick Button "Create post":

btn_invia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                progress.setVisibility(View.VISIBLE);
                postId = "";

                String [] charsForId = new String[] {"a", "b", "c", "d", "e", "f", "z", "3", "*", "#", "%", "&", "^", "r", "!", "j", "h", "q"}; //18 chars

                for(int i = 0; i<20; i++){
                    postId += charsForId[(int) Math.floor((Math.random() * 17) + 0)];
                }
                Toast.makeText(posta.this, postId, Toast.LENGTH_SHORT).show();
                descriz = descr.getText().toString();
                titolo = title.getText().toString();
                valut = vote.getText().toString();

                uploadImage();
                uploadDataOnFirestore(postId, descriz, titolo, valut, genre);




            }
        });

Could anyone help me with fixing this bug? I leave you the complete code of the activity in case I was not clear in describing my code: https://codeshare.io/eV9zE4

Upvotes: 0

Views: 68

Answers (1)

Yatharth Ranjan
Yatharth Ranjan

Reputation: 471

The uploadImage() method seems to be run asynchronously and the uploadDataOnFirestore is run right after the uploadImage without waiting for thee image to be uploaded, hence the image is null.

To fix this, move the uploadDataOnFirestore method call in the OnCompleteListener onComplete method of the uploadImage task, so that the image has completed uploading before running the uploadDataOnFirestore

Upvotes: 2

Related Questions