2

I tried all examples both from the docs and from online help, but I can't get a simple image I have in the storage to display on an image view.

assuming I have pictures in the storage in a folder called pictures so photo1.jpg is stored in the reference:

StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference photoReference= storageReference.child("pictures/photo1.jpg");

But how do I set it and replace the contents with an existing image view that I find by id:

ImageView photoView= (ImageView) findViewById(R.id.photo_view);

I keep getting failure on the listener to get Uri:

storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        profileImage.setImageURI(uri);
        Log.d("Test"," Success!");
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        Log.d("Test"," Failed!");
    }
});
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
pileup
  • 1
  • 2
  • 18
  • 45
  • Try putting your retrieved `uri` to log, maybe you're getting the wrong uri, from the storage. – PradyumanDixit Dec 05 '18 at 03:47
  • Hey @LesserFlow, do mark the answer as correct by clicking on tick mark looking V type button next to the answer, it should turn green. This helps future readers of this question and I'd appreciate that too. Cheers! :) – PradyumanDixit Dec 10 '18 at 05:36

3 Answers3

3

Using below code you can set image on ImageView:

StorageReference storageReference = FirebaseStorage.getInstance().getReference();
StorageReference photoReference= storageReference.child("pictures/photo1.jpg");

                final long ONE_MEGABYTE = 1024 * 1024;
                photoReference.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                    @Override
                    public void onSuccess(byte[] bytes) {
                        Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        imageView.setImageBitmap(bmp);

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Toast.makeText(getApplicationContext(), "No Such file or Path found!!", Toast.LENGTH_LONG).show();
                    }
                });

You can use the Uri as well, for that you need to save the byte array to a file. Use below code for the same:

File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();
Vikash Bijarniya
  • 404
  • 4
  • 10
1

There may be something going wrong with the uri you have retrieved from the Firebase Storage, try putting it on log, like this:

Log.d("uri:",uri.toString());

This may give you a hint, what is going wrong with retrieving the value. If you choose to use Glide or Picasso, then do refer their GitHub pages from the links above, and see what you need.

Using Glide and Picasso both is simple, and here's an example of how to use Glide to store the images in your imageView.

   // Reference to an image file in Cloud Storage
StorageReference storageReference = = FirebaseStorage.getInstance().getReference().child("yourImageReferencePath");


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

// Load the image using Glide
Glide.with(this /* context */)
        .using(new FirebaseImageLoader())
        .load(storageReference)
        .into(image );

If you don't want to use any external library and want to retrieve the image, then do refer this answer, it might help.

PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
0

Try to get the download URL of the image that you have uploaded in the database and use Picasso or GLIDE library to load it into the imageview you desire.

david
  • 3,225
  • 9
  • 30
  • 43
Konaru
  • 1
  • 1
  • I get failure on the success listener, I added the code to the post – pileup Dec 04 '18 at 16:58
  • 1
    Maybe because you are using storageReference to get the download URL. storageReference only gets the instance of the firebase storage. You should replace the storageReference.getDownloadUrl() to photoReference.getDownloadUrl(). – Konaru Dec 04 '18 at 18:34