Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – How to insert image URL into firestore database after uploading an image in android studio?

Posted on November 14, 2022

Solution 1 :

The file path cannot be used like that, as a reference to download an image. Instead you need to get a download url that is provided by firebase, change your code like this:

 StorageReference ref = storageReference.child("Users Profile/" + UUID.randomUUID().toString());
        UploadTask uploadTask = ref.putFile(filePath);

        Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                if (task.isSuccessful()) {
                    //here the upload of the image finish
                }

                // Continue the task to get a download url
                return ref.getDownloadUrl();
            }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult(); //this is the download url that you need to pass to your database
                    //Pass the url to your reference
                    UID = fAuth.getCurrentUser().getUid();
                    DocumentReference documentReference = fStore.collection("users").document(UID);
documentReference.update("profileImage", downloadUri);
                    Toast.makeText(ProfileActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                } else {
                    / Handle failures
                    // ...
                }
            }
        });

You can see in more detail here: https://firebase.google.com/docs/storage/android/upload-files#java_1

Solution 2 :

Your code doesn’t actually write anything to the DocumentReference you created. You will need a call to set():

DocumentReference documentReference = fStore.collection("users").document(UID);
documentReference.set(...)

You will need to pass an object or Map to set to tell it what to store in the document, as illustrated in the documentation.

Problem :

I created an upload image functionality in my app. The image uploads and shows for the user and returns “uploaded” upon success.

Now when the user closes the app and opens again it wont show the image because it is not stored in his unique database file in the field of profileImage.

I am storing the image in the storage datastore.

I want to store the same image url that is being stored in the storage to be in the current users field in the database.

READ  [ANSWERED] android - espresso how do you check if a dialog isn't visible
Powered by Inline Related Posts

But the code I’ve written still doesn’t insert the required value of the image url.

Here is my code so far:

FirebaseFirestore fStore;
FirebaseStorage storage;
StorageReference storageReference;

FirebaseAuth fAuth;
String UID;
private Uri filePath;
private final int PICK_IMAGE_REQUEST = 71;


fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
storage = FirebaseStorage.getInstance();
storageReference = storage.getReference();



protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    userImage = findViewById(R.id.profile_userImg);

    userImage.setOnClickListener(new View.OnClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
        @Override
        public void onClick(View view) {
            chooseImage();
        }
    });

}



@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
private void chooseImage() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Choose a Profile Image"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

        filePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            userImage.setImageBitmap(bitmap);
            if (filePath != null) {
                StorageReference ref = storageReference.child("Users Profile/" + UUID.randomUUID().toString());
                ref.putFile(filePath).addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        UID = fAuth.getCurrentUser().getUid();
                        DocumentReference documentReference = fStore.collection("users").document(UID);
                        Map < String, Object > user = new HashMap < > ();
                        user.put("profileImage", PICK_IMAGE_REQUEST);
                        Toast.makeText(ProfileActivity.this, "Uploaded", Toast.LENGTH_SHORT).show();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(ProfileActivity.this, "Failed", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Comments

Comment posted by Xenolion

you real have a lot of problems in the code: onSuccess method does not do anything there. First: you have made a map but you have added PICK_IMAGE_REQUEST which is just an integer “71”. Second. The app that you have made you have not used it all. You need a lot of code fix first.

Comment posted by Oscar Alejandro Alvarez

If you does not have your user document already created you need to use the method .set(), the method .update() only works if the field “profileImage” already exist.

READ  [ANSWERED] There is no single replacement for the Transform API - android?
Powered by Inline Related Posts

Comment posted by Doug Stevenson

All I’m saying is that you never actually wrote a document in your code. You will need to figure out what you want to write to the document and code that up. I don’t know what exactly you’re trying to write. The documentation should help you formulate the object to pass to set() that will do what you want.

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress