简体   繁体   中英

How to set image view from Firebase storage?

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

尝试获取已上传到数据库中的图像的下载URL,然后使用Picasso或GLIDE库将其加载到所需的imageview

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.

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

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