无法从Firebase存储在ImageView中显示图像 [英] Unable to Show Image In ImageView From Firebase Storage

查看:91
本文介绍了无法从Firebase存储在ImageView中显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,允许用户将图像上传到Firebase存储桶,然后获取图像文件的下载URL并将其添加到Firebase数据库.URL的格式为:-https://firebasestorage.googleapis.com/v0/b/bucket_name.appspot.com/o/filename?alt=media&token=token

I have an app which allows the users to upload the images to a Firebase bucket,then I get the download URL of the image file and add it to a firebase database.The URL is in the form :- https://firebasestorage.googleapis.com/v0/b/bucket_name.appspot.com/o/filename?alt=media&token=token

然后我尝试使用Glide将图像加载到RecyclerView中,但无法这样做,并且它无法显示图像.

Then I try to load the images into an RecyclerView using Glide but I am unable to do so and It fails to display the image.

如何获取以.png或.jpeg等结尾的绝对图像URL,据此可以轻松地加载图像.据我所知,问题出在url

How can I get an absolute Image URL which ends in .png or .jpeg etc which can be easily used to load the image.As far as I know the problem is with the url

推荐答案

从相机获取图像

                    Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(i, 3);

获取图像的文件路径

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_FILE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

      String  path=String.valueOf(data.getData());
      file = Uri.fromFile(new File(path));

创建文件元数据

  metadata = new StorageMetadata.Builder()
    .setContentType("image/jpeg")
    .build();

将文件和元数据上传到路径"images/avator.jpg"

Upload file and metadata to the path 'images/avator.jpg'

  uploadTask = storageRef.child("images/"+file.getLastPathSegment()).putFile(file, metadata);

收听状态更改,错误和上载完成.

Listen for state changes, errors, and completion of the upload.

  uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
    double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
    System.out.println("Upload is " + progress + "% done");
}
}).addOnPausedListener(new OnPausedListener<UploadTask.TaskSnapshot>() {
@Override
public void onPaused(UploadTask.TaskSnapshot taskSnapshot) {
    System.out.println("Upload is paused");
}
}).addOnFailureListener(new OnFailureListener() {
 @Override
public void onFailure(@NonNull Exception exception) {
    // Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    // Handle successful uploads on complete
    Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();

}

});

要使用Glide库加载图像

To load the Image using Glide Library

  Glide.with(getActivity())
.load(new File(downloadUrl)) // Uri of the picture
.into(Imageview);

这篇关于无法从Firebase存储在ImageView中显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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