【问题标题】:Glide not loading images while working with firebase cloud [duplicate]使用firebase云时滑翔不加载图像[重复]
【发布时间】:2020-05-10 18:24:44
【问题描述】:

所以,我正在开发可以加载存储在 Firebase 云中的图像的小型聊天应用程序。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RC_SIGN_IN) {
        // Code for text message
    }
        else if(requestCode==RC_PHOTO_PICKER && resultCode==RESULT_OK){
            Uri selectedImageUri=data.getData();
            StorageReference photoRef=mChatPhotoReference.child(selectedImageUri.getLastPathSegment());
            photoRef.putFile(selectedImageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    String downloadUrl=taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
                    FriendlyMessage friendlyMessage=new FriendlyMessage(null,mUsername,downloadUrl);
                    mDatabaseReference.push().setValue(friendlyMessage);
                    Log.e("WWWWWWWWWWWWWWWWWWWWWWW",downloadUrl);

                }
            });
    }

}

这里是获取视图。

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.item_message, parent, false);
    }

    ImageView photoImageView = (ImageView) convertView.findViewById(R.id.photoImageView);
    TextView messageTextView = (TextView) convertView.findViewById(R.id.messageTextView);
    TextView authorTextView = (TextView) convertView.findViewById(R.id.nameTextView);

    FriendlyMessage message = getItem(position); 

//FriendlyMessage是存储文本、用户名和图片url的类

    boolean isPhoto = message.getPhotoUrl() != null;
    if (isPhoto) {
        Log.e("VVVVVVVVVVVVVVVVVVVV",message.getPhotoUrl());
        messageTextView.setVisibility(View.GONE);
        photoImageView.setVisibility(View.VISIBLE);
        Glide.with(photoImageView.getContext())
                .load(message.getPhotoUrl())
                .into(photoImageView);
    } else {
          Load text..
    }
    authorTextView.setText(message.getName());

    return convertView;
}

我正在使用

glidle : 4.11.0 firebase-storage:19.1.0

【问题讨论】:

  • 请检查您是否在内部收到message.getPhotoUrl()
  • @Ashish 是的,我找到了它并得到了正确的值。
  • @AmanSharma 尝试在浏览器中打开它
  • @AmanSharma 这就是 glide 无法访问的原因。你需要这样的网址https://firebasestorage.googleapis.com/v0/b/xxxxApp.appspot.com/o/logo.png?alt=media&amp;token=c0472xxxxxxxxx88c44。它需要Token

标签: android firebase firebase-storage android-glide


【解决方案1】:

尝试使用 RequestOptions 应该看起来像

RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher_round)
                    .error(R.mipmap.ic_launcher_round);



 Glide.with(this)
    .load(image_url)
    .apply(options)
    .into(imageView);

如果你没有得到 url,你将显示错误图像,在这种情况下是 ic_lanucher_round

【讨论】:

  • 我这样做了,但没有显示图标。但是我记录了网址,它们的格式为 com.google.android.gms.tasks.zzu@d4cf1xx
  • 尝试将内部视图更改为 if (convertView == null) { final LayoutInflater layoutInflater = LayoutInflater.from(context); convertView = layoutInflater.inflate(R.layout.item_message, false); }
  • 解决了,问题是:taskSnapshot.getMetadata().getReference().getDownloadUrl() 返回任务对象而不是Uri @kyluAce
【解决方案2】:

您收到的网址不包含图片,它仍在上传。 你需要做这样的事情。 这里的while循环会让它等待,直到图片上传完毕。

 storageRef.child(UUID.randomUUID().toString()).putFile(uri)
                .addOnSuccessListener { p0 ->
                    val downloadUrl = p0!!.storage.downloadUrl
                    @Suppress("ControlFlowWithEmptyBody")
                    while (!downloadUrl.isSuccessful);

                    try {
                        val imagePath = downloadUrl.result!!.toString()
}

它在 Kotlin 中,您可以轻松翻译。

【讨论】:

  • 但是当我通过网站检查时,图像显示正确。
  • 您尝试获取的网址显示不正确,请尝试按照我获取的方式获取它,然后它将在您的应用中正确显示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 2017-07-12
  • 1970-01-01
  • 2017-07-14
  • 2020-02-03
相关资源
最近更新 更多