所以,我正在研究小型聊天应用程序,该应用程序可以加载存储在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
尝试使用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
您接收的网址不包含图片,它仍在此处上传。您需要做这样的事情。在这里,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,您可以轻松翻译。