Android WebView按钮输入文件

时间:2014-08-11 15:27:08

标签: java android html webview

抱歉我的英文..我有一个网络项目,我将在app中使用android,但在应用程序的一部分必须使用input type =“file”,但它在Android Webview中不起作用,试图点击“选择一个文件”它只是静态的并且没有打开,但是使用浏览器网址它完美无缺。已经尝试了几种方法来解决这个问题,但没有成功。 有人知道我该如何解决..请让我知道......

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

}

2 个答案:

答案 0 :(得分:2)

找到适用于我的解决方案。再添加一个proguard规则:

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

答案 1 :(得分:0)

您需要在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 );

           }

    });