0

I am trying to download an image from Firebase storage into imageView on an android studio app using Java. I have looked through stackoverflow for over 6 hours so I have most likely read any "similiar" questions. I have tried various methods but the most suggested and what appeared to be the most simplest is using glide. I can't get it to work and I can't find the error. I even put in a regular google images link to try and it still shows blank.

the dependencies I have added

implementation'com.firebaseui:firebase-ui-storage:6.4.0'
implementation'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor'com.github.bumptech.glide:compiler:4.12.0'

The code I have used

ImageView imageView = findViewById(R.id.resultImage);
Glide.with(this).load("string of image").into(imageView);

I also tried this code

StorageReference ref = storage.getReferenceFromUrl("urlforimage");

// ImageView in your Activity
ImageView imageView = findViewById(R.id.uploadedImage);

// Download directly from StorageReference using Glide
Glide.with(this /* context */)
    .load(ref)
    .into(imageView);

And this code

public class Breed_Search_Page1Activity extends AppCompatActivity {
ImageView rImage;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_breed__search__page1);
    
    rImage = findViewById(R.id.rImage);
    
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    
    DatabaseReference databaseReference = firebaseDatabase.getReference();
    
    DatabaseReference getImage = databaseReference.child("image");
    
    getImage.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String link = dataSnapshot.getValue(String.class);
            Picasso.get().load(link).into(rImage);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            // we are showing that error message in toast
            Toast.makeText(Breed_Search_Page1Activity.this, "Error Loading Image", Toast.LENGTH_SHORT).show();
        }
    });
}
}

Update - I have it working with the code below but wondering is there a way to display it from Firebase Storage using just the name of the file? As I am uploading an image from my laptop that I want to display in the app as quickly as possible so if I could code it so that it has the file name that I am going to use when uploading it would display once uploaded without having to manually change anything

ImageView image = (ImageView) findViewById(R.id.rImage);

String url = "https//...";
Glide.with(getApplicationContext()).load(url).into(image);

My logcat after adding listeners

2021-04-27 10:35:01.780 14347-14576/com.example.woofsapp E/StorageException: 
StorageException has occurred.
    Object does not exist at location.
 Code: -13010 HttpResult: 404
2021-04-27 10:35:01.781 14347-14576/com.example.woofsapp E/StorageException: 
{  "error": {    "code": 404,    "message": "Not Found.  Could not get 
object",    "status": "GET_OBJECT"  }}
    java.io.IOException: {  "error": {    "code": 404,    "message": "Not 
Found.  Could not get object",    "status": "GET_OBJECT"  }}
    at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:434)
    at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:451)
    at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:442)
    at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
    at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:286)
    at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:70)
    at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:62)
    at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:76)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:764)

The code that gives the logcat errors - note I do have the MyAppGlideModule copied directly from documentation

ImageView image = findViewById(R.id.rImage);
    StorageReference storageReference = 
FirebaseStorage.getInstance().getReference("Images/dogs.jpg");

    
storageReference.child("dogs.jpg").getDownloadUrl().addOnSuccessListener(new 
OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'users/me/profile.png'
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });
tech123
  • 13
  • 1
  • 7
  • what error you are getting from your log cat? – Ritt Apr 25 '21 at 16:10
  • These are the only two things in red in my logcat - 2021-04-26 09:53:44.847 12063-12063/? E/Zygote: isWhitelistProcess - Process is Whitelisted 2021-04-26 09:53:44.848 12063-12063/? E/Zygote: accessInfo : 1 These are at the very beginning of the logcat and after researching them I understand they are not really an error. No error shows in the logcat when I press the button that should bring me to a new page with the image – tech123 Apr 26 '21 at 08:54

1 Answers1

1

In my app, I've something like this:

StorageReference storageReference = FirebaseStorage.getInstance().getReference("FolderName/photoName.format");

I'm using the path with file's name.

For reference, check the official documentation - Download files with Cloud Storage on Android.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
amtrax
  • 466
  • 7
  • 20
  • Would val be replaced with StorageReference storageReference? Then I would put storageReference in place of url in my code? – tech123 Apr 26 '21 at 15:04
  • 1
    @tech123 Replace val with `StorageReference`, the type of the variable, the code shared above is of Kotlin. – Lalit Fauzdar Apr 26 '21 at 15:05
  • I added it and it hasn't worked. My images are not in a folder they are just in storage so do I need anything before the name of the image? I added this code ImageView image = (ImageView) findViewById(R.id.rImage); StorageReference storageReference = FirebaseStorage.getInstance().getReference("dogs.jpg"); Glide.with(getApplicationContext()).load(storageReference).into(image);'= – tech123 Apr 26 '21 at 15:09
  • @tech123 in that case just proper file name with format – amtrax Apr 26 '21 at 15:09
  • @amtrax it is not working, I edited my above comment to add the code, have I missed out on something? – tech123 Apr 26 '21 at 15:11
  • @tech123 it's hard to say. Be sure that your reference is good. You can also try to get that photo without glide, for testing purposes, so we can say where the problem is. Check the documentation link launched in edited post. Keep us informed, good luck – amtrax Apr 26 '21 at 15:16
  • 1
    @tech123 The way you're loading is correct, there's some issue with your file, is it publicly accessible? are the security rules set-up correctly? See, Firebase's official documentation [here](https://github.com/firebase/FirebaseUI-Android/blob/master/storage/README.md#usage) and [here](https://firebase.google.com/docs/storage/android/download-files#downloading_images_with_firebaseui), they've used the exact same way. – Lalit Fauzdar Apr 26 '21 at 15:20
  • @LalitFauzdar I have Firebase storage rules set to read, write if true and I have set the bucket as public to the internet – tech123 Apr 26 '21 at 15:34
  • @amtrax the reference is definitely right because I have no folders in my Firebase storage and there is a file called dogs.jpg so my reference is StorageReference storageReference = FirebaseStorage.getInstance().getReference("dogs.jpg");. I had looked at the Firebase documentation previously and couldn't find a resolution. After you have the storageReference in your app how are you calling it? – tech123 Apr 26 '21 at 15:50
  • @tech123 I really don't know where the problem's, maybe try to create folder and put this file inside. Real Solution that appears to me's to use failure listener and then read the log. – amtrax Apr 26 '21 at 16:01
  • @amtrax I already tried adding it to a folder and unfortunately it didn't work. In my original post I tried onDataChanged and onCancelled which didn't show any error in my log. Is a failure listener different to this? – tech123 Apr 26 '21 at 16:09
  • @tech123 Slighty differrent method, but should show us possibly failure. https://firebase.google.com/docs/storage/android/download-files#download_data_via_url It's also important to implement class that extends AppGlideModule when using Glide this has been show here: https://firebaseopensource.com/projects/firebase/firebaseui-android/storage/readme/ – amtrax Apr 26 '21 at 16:19
  • @amtrax I added the logcat after adding listeners to my original post so that it was easier to read. My Firebase storage has no folders and just one file called dogs.jpg, do you have any idea why this error is occurring? – tech123 Apr 27 '21 at 09:39
  • @tech123 Did you try with other files? try with this permissions: ` ` What you can do is to replace UI dependencies with original ones, or try to do the same on fresh new project. Check this: https://stackoverflow.com/questions/42982147/storageexception-has-occurred-object-does-not-exist-at-location-storage-fireba – amtrax Apr 27 '21 at 10:24
  • I ended up using picasso as suggested in this post [link] (https://stackoverflow.com/questions/62618469/picasso-is-not-working-in-android-10-why) and got it to display – tech123 Apr 28 '21 at 15:46