0

Just to preface this post, I am pretty new at coding and I like to learn by doing. But when I can't do something, I look online for answers, ask questions, or brute force it.

So my question is what is the order of operations behind firebase image storage and retrieval on both a device and from firebase's storage?

What I am trying to do in my app is very simple actually, but it is giving me a hard time since I don't really understand how it works and I don't like just copying code unless I could do most of it myself. I already have down the basics of logging in a user with an email, password and storing strings to the database, but when it comes to images or anything stored locally, I'm lost. My understanding of the order of operations is as follows. PLEASE correct me if I am wrong:

1) Data is accessed locally, the profile image in that particular instance is then set to the Image View, and finally, a new variable is created which is of class Uri. The Uri class creates a variable that references the particular location of the file on the device. All of this is done via the code below:

mProfileImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 1);
            }
        });

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == Activity.RESULT_OK){
            final Uri imageUri = data.getData();
            resultUri = imageUri;
            mProfileImage.setImageURI(resultUri);
        }
    }

2. Subsequently, the Uri data needs to be/is converted into a string

3. This Uri String is then put into a Map class variable and entered into the database under the current user.

4. In order to store the actual image, (I'm using Glide btw) you have to create a StorageReference variable and then convert the Uri image reference into a Bitmap variable and upload it into firebase storage via the following code:

(Create the Bitmap variable)

    Bitmap bitmap = null;
            try {bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), 
                    resultUri);
                bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), 
                    resultUri);
            }catch (IOException e){
                e.printStackTrace();
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
            byte[] data = baos.toByteArray();

            UploadTask uploadTask = filepath.putBytes(data);
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    finish();
                }
            });

(The upload part)

UploadTask uploadTask = filepath.putBytes(data);
            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    finish();
                }
            });

            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Uri downloadUrl = Uri.parse(taskSnapshot.getMetadata().getReference().getDownloadUrl().toString());
                    Map userInfo = new HashMap();
                    userInfo.put("profileImageUrl", downloadUrl.toString());
                    mUserDatabase.updateChildren(userInfo);

                    finish();
                    return;

                }
            });

5. At this point the A String of the Uri reference is associated with the particular user and the actual image is stored in Firebase Storage associated with the corresponding UID.

6. After this, there are two different scenarios:

Scenario 1: User logs into the app automatically from the same device they uploaded the image from (a cellphone in my case). The user then navigates to the profile page. When the profile page is accessed the app code must check several different things. First, is the image already located on the device? Which it checks by accessing the firebase Database under the particular user, taking the String of the Uri stored there and then converting it back into a Uri variable and then looks for the image on the device. Once the image is found, it will place that image into the ImageView.

Scenario 2: User does the exact same thing but from a device without the image already on it. App still first gets the String of the Uri from the Firebase Database, converts it back into a Uri variable and then searches the device. Since this is a new device, the image isn't there so the app (in this case via Glide) loads the image from the Firebase Storage associated with the User into the profile photo and subsequently saves it onto the device itself. Future instances on this new device will then follow scenario 1.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • You're just not using getDownloadUrl correctly. See the dup, and be sure to read the documentation as well. – Doug Stevenson Mar 16 '20 at 22:52
  • Thank you for noticing that, but I actually wanted to know about the process, not the code. –  Mar 17 '20 at 01:51

0 Answers0