【问题标题】:Android WebView button input fileAndroid WebView 按钮输入文件
【发布时间】:2014-08-11 15:27:08
【问题描述】:

对不起我的英语.. 我有一个 web 项目,我将在应用程序中使用 android,但在应用程序的一部分必须使用 input type="file",但它在 Android Webview 中不起作用,当尝试单击“选择文件”它只是静态的并且不会打开打开,但是使用浏览器 url 它可以完美运行。已经尝试了几种方法来解决这个问题,但没有成功。 任何人都知道我该如何修复.. 请让我知道...

public class MainActivity extends Activity {

    private String URL = "http://localhost:8080/myproject/upload.html";
    private WebView webView;
    private ValueCallback<Uri> uploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;
    private static String registrationId = "";

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (uploadMessage == null) return;
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            uploadMessage.onReceiveValue(result);
            uploadMessage = null;
        }
    }

}

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        webView = (WebView) findViewById(R.id.container);
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient()  
        {  

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

                Log.i("openFileChooser", " IN ");

                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult(Intent.createChooser(i,"File Chooser"), 222);  
            }


            @SuppressWarnings("unused")
            public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
                //             Intent i = new Intent(Intent.ACTION_GET_CONTENT);
                Log.i("openFileChooser", " IN ");
                i.addCategory(Intent.CATEGORY_OPENABLE);
                i.setType("*/*");
                startActivityForResult(Intent.createChooser(i, "File Browser"), 222);
            }


            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
                // openFileChooser(uploadMsg);
                Log.i("openFileChooser", " IN ");
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult( Intent.createChooser( i, "File Chooser" ), 2223 );

            }

        }); 

        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl(URL);

}

【问题讨论】:

    标签: java android html webview


    【解决方案1】:

    找到了一个适合我的解决方案。再添加一条 proguard 规则:

    -keepclassmembers class * extends android.webkit.WebChromeClient {
         public void openFileChooser(...);
    }
    

    【讨论】:

    • 嗨!请提供最终代码,因为我被困在同一点上......谢谢!
    【解决方案2】:

    您需要在WebChromeClient 中设置一些权限才能达到相同的目的。

    webView.setWebChromeClient(new WebChromeClient()  
        {  
    
            //The undocumented magic method override  
               //Eclipse will swear at you if you try to put @Override here  
            // For Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                i.addCategory(Intent.CATEGORY_OPENABLE);  
                i.setType("image/*");  
                startActivityForResult(Intent.createChooser(i,"File Chooser"), 222);  
               }
    
            // For Android 3.0+
               public void openFileChooser( ValueCallback uploadMsg, String acceptType ) {
    //           Intent i = new Intent(Intent.ACTION_GET_CONTENT);
               i.addCategory(Intent.CATEGORY_OPENABLE);
               i.setType("*/*");
               startActivityForResult(
               Intent.createChooser(i, "File Browser"),
               222);
               }
    
            //For Android 4.1
               public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
    //               openFileChooser(uploadMsg);
                   Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                   i.addCategory(Intent.CATEGORY_OPENABLE);  
                   i.setType("image/*");  
                   startActivityForResult( Intent.createChooser( i, "File Chooser" ), 2223 );
    
               }
    
        }); 
    

    【讨论】:

    • 你会在 onActivityresult 中得到结果,然后你需要调用类似 webView.uploadfile();
    • 我实现了同样的。但在 4.3 或 4.4 中存在一些特定于版本的问题。它在三星标签 4.1 中工作。
    猜你喜欢
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2015-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多