Unable to set Firebase Image Uri in Image View

匿名 (未验证) 提交于 2019-12-03 01:34:02

问题:

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

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.

Just image2.setImageURI(uri) is not working.

回答1:

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'

And to load your image:

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


回答2:

You can also use Picasso library. Add this to your gradle

compile 'com.squareup.picasso:picasso:2.5.2'

Then to load the image,

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!