Firebase存储不会在webview上显示图像

时间:2018-06-01 10:20:07

标签: android firebase google-cloud-firestore firebase-storage

我将html数据上传到FireStore。我使用该数据在webview上显示。 但是webview不会加载图像

以下是我用来将html文本上传到FireStore的代码

 if (editor.getContentAsHTML().contains("<img")) {

        if (mAuth.getCurrentUser() != null) {
            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();
                        }
                    });
                }
            });
        }


    }

我在recyclerview中显示这些数据。 在问题集合中,我检索数据并使用recyclerview在webview上显示 以下代码用于从问题集合中检索数据

question_modelList = new ArrayList<>();

    firebaseFirestore = FirebaseFirestore.getInstance();
    questionAdapter = new QuestionAdapter(question_modelList,getApplicationContext());
    question_list.setLayoutManager(new LinearLayoutManager(this));
    question_list.setAdapter(questionAdapter);
    if (firebaseFirestore != null) {
        try {
            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) {

                        }
                    }
                }
            });
        } catch (Exception e) {

        }
    }

这是我的适配器

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 个答案:

没有答案