简体   繁体   中英

Image showing issue in firebase storage project

I have setup firebase project and I can get the chat messages exchange between two users successfully but I am not able to show the profile image for users. When I open this url in web browser image is shown, but if I use this url in android phone, image not showing.

I got the download URL as below: https://firebasestorage.googleapis.com/v0/b/fir-chat-be49b.appspot.com/o/q4PEL3jJKsWeK0yEyIJuwNMkUmC2%2Fphotourl%2Fimage%3A11254?alt=media&token=bf97c5de-488f-4e05-b282-3789b4be27e0

I am uploading image from another activity, then I try to get images from uploading images URL from another activity.

Below is my code which I used to retrieve image and other data from Firebase database. I successfully got URL of uploaded images and pass them in adapter, where I try to use Picasso or Glide to show that image by using this URL.

By that URL image show successfully in Web Browser, but unable to show this image in imageview in android.

DatabaseReference mFirebaseDatabaseReference=FirebaseDatabase.getInstance().getReference();

mFirebaseDatabaseReference.child(USERPROFILE_CHILD).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if(FirebaseAuth.getInstance().getCurrentUser().getEmail().equalsIgnoreCase(dataSnapshot.child("email").getValue().toString())) {

            } else {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("email", dataSnapshot.child("email").getValue().toString());
                try {

// Got uploaded image url by using dataSnapshot.child("photourl").getValue() and put it in hashMap
                    hashMap.put("photourl",dataSnapshot.child("photourl").getValue().toString());

                } catch (Exception e) {
                    e.printStackTrace();
                }
                hashMap.put("uid", dataSnapshot.child("uid").getValue().toString());
                hashMap.put("firebaseToken",dataSnapshot.child("firebaseToken").getValue().toString());
                testData.add(hashMap);
                adapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

This is my custom adaptor code to load image in imageview:---

final ImageView profileimage = (ImageView) v.findViewById(R.id.profileImageView);

    try {
        String imageUrl = data.get(position).get("photourl");
        if (imageUrl.startsWith("gs://")) {
            final StorageReference storageReference = FirebaseStorage.getInstance()
                    .getReferenceFromUrl(imageUrl);
            storageReference.getDownloadUrl().addOnCompleteListener(
                    new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                            if (task.isSuccessful()) {
                                String downloadUrl = task.getResult().toString();
                      System.out.println("====download url===="+downloadUrl);

                                /*Glide.with(context)
                                        .load(downloadUrl)
                                        .into(profileimage);*/                                    Picasso.with(context).load(downloadUrl).into(profileimage);
                   } else {
                Log.w("thank you", "Getting download url was not successful.",
                                        task.getException());
                            }
                        }
                    });
        } else {
            Picasso.with(context)
                    .load(imageUrl)
                    .into(profileimage);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Here you go.!

Go to Firebase Rules First

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
   allow read, write; //here You Are Allowing everyone to read and write
    }
  }
}


service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null; //only auth person k read and write
    }
  }
}

Then Add Glide or picasso library.!

compile 'com.squareup.picasso:picasso:2.5.2'

  compile 'com.github.bumptech.glide:glide:3.6.1'

    compile 'com.android.support:support-v4:25.1.0'

  StorageReference filePath;

     StorageReference mFStorage;


     mFStorage=FirebaseStorage.getInstance().getReference();

     String Id= UUID.randomUUID().toString();


       filePath=   mFStorage.child("images").child(Id+".png");


    filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

   String imageDownloadUrl=taskSnapshot.getDownloadUrl();


   Glide.with(ctx)
                    .load(imageDownloadUrl)
                    .into(imageview);

     Picasso.with(context).load(imageDownloadUrl.toString()).into(imageview);

try to use Picasso or Glide It will load Your Image .!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM