【问题标题】:How can I use the HTML file input in android studio webview?如何在 android studio webview 中使用 HTML 文件输入?
【发布时间】:2017-04-07 01:06:49
【问题描述】:

我正在尝试制作基于 webview 的应用程序。到目前为止一切正常,除了当我点击文件输入时没有任何反应。 我在互联网上搜索了许多解决方案,但没有一个对我有用,即使是这个问题的那些:File Upload in WebView 这是我的 MainActivity.java 代码:

package com.myapp.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    WebView wv;
    private ProgressBar progressBar;
    @Override
    public void onBackPressed(){
        if(wv.canGoBack()){
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wv = (WebView) findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusable(true);
        wv.setFocusableInTouchMode(true);
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wv.getSettings().setDomStorageEnabled(true);
        wv.getSettings().setDatabaseEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
        wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        wv.loadUrl("https://myapp.com");
        wv.setWebViewClient(new WebViewClient());

        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        wv.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int progress)
            {
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                MainActivity.this.setProgress(progress * 1000);

                progressBar.incrementProgressBy(progress);

                if(progress == 100){
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }
}

【问题讨论】:

  • 什么错误?你可以发布你的 logcat 吗?
  • 即使这个问题File Upload in WebView 也不起作用?您是否“覆盖”了从 android 2.3+ 到 5.0+ 的所有 openFileChooser 版本?并且,正确处理 onActivityResult ?
  • openFileChooser 是灰色的,并说:从未使用方法“openFileChooser(android.webkit.ValueCallback, java.lang.String)”。我认为这是错误,但我不知道如何解决它。我是新手。

标签: android html file android-studio webview


【解决方案1】:

你必须在WebChromeClient中覆盖openFileChooser

      public void openFileChooser(ValueCallback<Uri> uploadFile) {    
                            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                            i.addCategory(Intent.CATEGORY_OPENABLE);  
                            i.setType("*/*");  
                            startActivityForResult(Intent.createChooser(i, getString(R.string.choose_upload)), FILE_SELECTED);  
       }  

在 WebChormeClient 类中添加此方法:

wv.setWebChromeClient(new WebChromeClient(){

            public void openFileChooser(ValueCallback<Uri> uploadFile) {    
                            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                            i.addCategory(Intent.CATEGORY_OPENABLE);  
                            i.setType("*/*");  
                            startActivityForResult(Intent.createChooser(i, getString(R.string.choose_upload)), FILE_SELECTED);  
            }  


            @Override
            public void onProgressChanged(WebView view, int progress)
            {
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                MainActivity.this.setProgress(progress * 1000);

                progressBar.incrementProgressBy(progress);

                if(progress == 100){
                    progressBar.setVisibility(View.GONE);
                }
            }
        });

【讨论】:

  • openFileChooser 是灰色的并且说: Method "openFileChooser(android.webkit.ValueCallback, java.lang.String)" is never used
  • openFileChooser 是灰色的并且说: Method "openFileChooser(android.webkit.ValueCallback)" is never used***
猜你喜欢
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 2023-03-10
  • 2015-04-03
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
相关资源
最近更新 更多