Android 无法在图像视图中设置Firebase图像Uri

Android 无法在图像视图中设置Firebase图像Uri,android,firebase,firebase-storage,Android,Firebase,Firebase Storage,我正在从firebase数据库检索图像,并在图像视图中进行设置。 我正在使用以下代码 mStorageRef.child("Book_Photos/"+firstBook.bid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { T

我正在从firebase数据库检索图像,并在图像视图中进行设置。 我正在使用以下代码

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
            }
        });
mStorageRef.child(“Book_Photos/”+firstBook.bid).getDownloadUrl().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时的公共无效(Uri){
Toast.makeText(getApplicationContext(),“获取图像成功”,Toast.LENGTH_LONG.show();
if(uri==null){
Toast.makeText(getApplicationContext(),“URI为空”,Toast.LENGTH_LONG.show();
}
试一试{
图像视图图像2;
image2=(ImageView)findViewById(R.id.ImageView);
image2.setImageURI(null);
image2.setImageURI(uri);
}
捕获(例外e){
}
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
Toast.makeText(getApplicationContext(),“获取图像失败”,Toast.LENGTH_LONG.show();
//处理任何错误
}
});
未设置我正在检索的图像。 “树立成功形象”的祝酒词很有效。 “URI为空”toast不起作用。 命令image2.setImageURI(null)可以工作


仅图像2。setImageURI(uri)不工作。

您无法将图像从internet直接加载到ImageView。但是你可以用图书馆来做这件事。要将Glide添加到应用程序中,您需要将此行添加到从属项中:

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

你也可以使用毕加索图书馆。把这个加到你的梯度上

编译'com.squareup.picasso:picasso:2.5.2'

然后加载图像

Picasso.with(YourActivity.this).load(uri).into(image2)


如果uri不是垃圾,我会使用Log命令
Log.d(“uri字符串”,uri.toString())
来查找。最容易使用。