无法在“图像视图"中设置Firebase Image Uri [英] Unable to set Firebase Image Uri in Image View

查看:92
本文介绍了无法在“图像视图"中设置Firebase Image Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Firebase数据库中检索图像并将其设置在图像视图"中. 我正在使用以下代码.

I am retrieving an image from my firebase database and setting it in an Image View. I am using the following code.

mStorageRef.child("Book_Photos/"+firstBook.bid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Toast.makeText(getApplicationContext(), "GET IMAGE SUCCESSFUL",Toast.LENGTH_LONG).show();
                if(uri==null){
                    Toast.makeText(getApplicationContext(), "URI IS NULL",Toast.LENGTH_LONG).show();
                }
                try {
                    ImageView image2;
                    image2=(ImageView)findViewById(R.id.imageView);
                    image2.setImageURI(null);
                    image2.setImageURI(uri);

                }
                catch (Exception e){

                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast.makeText(getApplicationContext(), "GET IMAGE FAILED",Toast.LENGTH_LONG).show();
                // Handle any errors
            }
        });

我正在检索的图像未设置. 获取图像成功"吐司可以工作. "URI IS NULL"吐司不起作用. 该命令image2.setImageURI(null)有效.

The image I am retrieving is not being set. The "GET IMAGE SUCCESSFUL" toast works. The "URI IS NULL" toast does not work. The command image2.setImageURI(null) works.

image2.setImageURI(uri)不能正常工作.

Just image2.setImageURI(uri) is not working.

推荐答案

您不能将图像直接从Internet加载到ImageView.但是您可以使用 Glide 之类的库来执行此操作.要将Glide添加到您的应用中,您需要将此行添加到您的依赖项中:

You can't load images from the internet directly to an ImageView. But you can use a library like Glide to do it. To add Glide to your app, you need to add this line to your dependecies:

implementation 'com.github.bumptech.glide:glide:4.8.0'

并加载图片:

Glide
    .with(getContext())
    .load(uri) // the uri you got from Firebase
    .centerCrop()
    .into(image2); //Your imageView variable

这篇关于无法在“图像视图"中设置Firebase Image Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆