我将一些html文本(包含img标签)上传到Firestore(图像上传到firebase存储)。当我试图将这些html数据显示在webview上时,我只获得文本,没有图像
以下是我用于将HTML上传到FireStore的代码
storageReference.child(System.currentTimeMillis() + ".png").putFile(file_uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
post_img_url = String.valueOf(taskSnapshot.getDownloadUrl());
String HTML = editor.getContentAsHTML().replace(uuid_, post_img_url);
Question_model question_model = new Question_model(mAuth.getUid(), HTML);
mFireStoredbReference.collection("Questions").add(question_model).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
@Override
public void onComplete(@NonNull Task<DocumentReference> task) {
Toast.makeText(Forum.this, "Question added", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Forum.this, ListQuestions.class));
finish();
}
});
}
});
我尝试从问题集合中获取此数据
firebaseFirestore.collection("Questions").addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e instanceof com.google.firebase.firestore.FirebaseFirestoreException) {
// Log.d(TAG, "onEvent: The error is " + e);
} else {
try {
for (DocumentChange doc : queryDocumentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
Question_model model = doc.getDocument().toObject(Question_model.class);
question_modelList.add(model);
questionAdapter.notifyDataSetChanged();
}
}
} catch (Exception e2) {
}
}
}
});
这是我的recyclerview适配器
public class QuestionAdapter extends RecyclerView.Adapter<QuestionAdapter.MyViewHolder> {
Context ctx;
FirebaseFirestore mFirebaseFirestore;
private List<Question_model> question_modelList;
public QuestionAdapter(List<Question_model> question_modelList, Context ctx) {
this.question_modelList = question_modelList;
this.ctx = ctx;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.question_individual, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
mFirebaseFirestore = FirebaseFirestore.getInstance();
String user_id = question_modelList.get(position).getUser_id();
mFirebaseFirestore.collection("users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
holder.user_name.setText(task.getResult().getString("user_name"));
} else {
}
}
});
Log.d("Asis", "onBindViewHolder: "+question_modelList.get(position).getContent_html());
holder.question.loadData(question_modelList.get(position).getContent_html(),"text/html","utf-8");
}
@Override
public int getItemCount() {
return question_modelList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView user_name;
WebView question;
public MyViewHolder(View itemView) {
super(itemView);
user_name = itemView.findViewById(R.id.user_name);
question = itemView.findViewById(R.id.question);
question.getSettings().setJavaScriptEnabled(true);
question.getSettings().setLoadsImagesAutomatically(true);
question.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
question.getSettings().setDomStorageEnabled(true);
}
}
}
答案 0 :(得分:0)
您可以使用Glide轻松地从Firebase存储中下载图像。
首先,将FirebaseUI添加到您的app / build.gradle:
dependencies {
// FirebaseUI Storage only
implementation 'com.firebaseui:firebase-ui-storage:0.6.0'}
然后您可以直接从存储中加载图像:
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(/*storageReference*/)
.into(/*yourView*/);