图像未显示在 webview 上

Posted

技术标签:

【中文标题】图像未显示在 webview 上【英文标题】:Image is not displayed on webview 【发布时间】:2018-11-11 10:04:12 【问题描述】:

我将一些 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();
                        
                    );
                
            );

我尝试从 Questions 集合中获取这些数据

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);

    



【问题讨论】:

请添加代码。并欢迎堆栈溢出。 请添加一些带有结果的代码。 【参考方案1】:

您可以使用 Glide 从 Firebase 存储轻松下载图像。首先,将 FirebaseUI 添加到您的 app/build.gradle:

dependencies 
// FirebaseUI Storage only
implementation 'com.firebaseui:firebase-ui-storage:0.6.0' 

然后就可以直接从Storage加载图片了:

Glide.with(this /* context */)
    .using(new FirebaseImageLoader())
    .load(/*storageReference*/)
    .into(/*yourView*/);

【讨论】:

以上是关于图像未显示在 webview 上的主要内容,如果未能解决你的问题,请参考以下文章

WebView 中的背景图像显示在模拟器中,但不在设备上

收藏的内容未在 webview 上正确显示

当设备在 react-native 中离线时,在 webview 中显示本地图像

Android:来自Url的Webview中显示的图像具有高质量损失

是否在 webview 中加载图像?

youtube 视频未在 webView 中显示