简体   繁体   中英

Downloading image from Firebase storage into imageview on android studio java

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
        }
    });

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 .

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