0
public class About extends AppCompatActivity {
ImageView imageView;
TextView textView;

String mediaUrL;
private  FirebaseFirestore firebaseFirestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_about);
    firebaseFirestore = FirebaseFirestore.getInstance();
    imageView = findViewById(R.id.showGraphImage);
    textView = findViewById(R.id.jkbdsjfdvshf);
    firebaseFirestore.collection("crimeData").document("graph").get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot snapshot) {
            mediaUrL = snapshot.getString("imageUrl");
            textView.setText(mediaUrL);
            System.out.println(snapshot.getString("imageUrl"));
        }
    });
    Glide.with(this).load(mediaUrL).into(imageView);
    System.out.println(mediaUrL);

I am new to android and I am using Cloud Firestore to store the data online .the variable mediaUrl prints on the console but don't show in the activity. I got the answer but still, now it is not working. Further, I added a used mediaUrl to display the image but it is also not showing on the activity.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
vijayrealdeal
  • 19
  • 1
  • 1
  • 7

4 Answers4

0

As Sahil Goyal wrote, you need to use TextView.

final TextView textView = findViewById<TextView>(R.id.textView);

    ...
    public void onSuccess(DocumentSnapshot snapshot) {
        mediaUrL = snapshot.getString("imageUrl")
        System.out.println(snapshot.getString("imageUrl"));
        textView.setText(mediaUrL);
    }
CoolMind
  • 26,736
  • 15
  • 188
  • 224
  • @vijayrealdeal, are logs written? Is `onSuccess` called and what data are inside mediaUrL, snapshot? – CoolMind Mar 13 '21 at 16:35
  • See also https://stackoverflow.com/questions/46706433/firebase-firestore-get-data-from-collection and others. There are examples of callbacks and data retrieving. – CoolMind Mar 13 '21 at 16:43
0

You must have to add an adapter item and initiate that with a template adapter item in order to work with it.

Make a adapter class and inside oninstantiateitem() function create a itemview -

val itemView: View = LayoutInflater.from(container.context).inflate(
        R.layout.pageadapteritem, container, false
    )

After you have created itemview set -

itemView.textView.setText(mediaUrl)

PS - This code is in Kotlin, kindly change it to java when android studio asks to do so while pasting.

idevesh
  • 155
  • 1
  • 9
0

The problem in your code lies in the fact that you are trying to set the image using the following lines of code:

Glide.with(this).load(mediaUrL).into(imageView);
System.out.println(mediaUrL);

Outside the "onSuccess()" callback. Any code that needs data from Cloud Firestore, needs to be inside the onSuccess method, or be called from there. So the simplest solution is to move both lines of code right after:

System.out.println(snapshot.getString("imageUrl"));

This is happening because Firebase API is asynchronous. For more info about this topic, please see my answer from the following post:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
0

If you want to show the image onto the ImageView, you should consider the following first:

1.- Please, double check if the image on your firebase has public access, you can set that permissions from the Google Cloud console through the shell console with this command.

gsutil acl ch -u AllUsers:R gs://<YOUR_BUCKET>/<IMAGE_NAME>

After that, to validate if your image has public access you can copy and paste the url string on a web browser, you must see your image in there.

2.- Also verify, if the path on the code to your image on firebase database has no typos and is correct. After checking the previous, If you see the “imageURL” value is printed on the console.

3.- You should move this line
Glide.with(getApplicationContext()).load(mediaUrl).into(imageView);

inside the onSuccess() method like this:

public void onSuccess(DocumentSnapshot snapshot) {
    mediaUrl = snapshot.getString("imageUrl");
    textView.setText(mediaUrl);
    Glide.with(getApplicationContext()).load(mediaUrl).into(imageView);
}

then it will be set on your imageView.

Good luck!

Mathew Bellamy
  • 113
  • 2
  • 10