Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android WebView按钮输入文件_Java_Android_Html_Webview - Fatal编程技术网

Java Android WebView按钮输入文件

Java Android WebView按钮输入文件,java,android,html,webview,Java,Android,Html,Webview,对不起,我的英语。。我有一个web项目,我将在android应用程序中使用,但在部分应用程序中必须使用input type=“file”,但它在android Webview中不起作用,当尝试单击“选择文件”时,它只是静态的,不会打开,但通过浏览器url它可以完美地工作。已经尝试了几种方法来解决这个问题,但没有成功。 谁都知道我该怎么做。。请让我知道 public class MainActivity extends Activity { private String URL = "h

对不起,我的英语。。我有一个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);
公共类MainActivity扩展活动{
专用字符串URL=”http://localhost:8080/myproject/upload.html";
私有网络视图;
私有值回调上传消息;
private final static int FILECHOOSER_RESULTCODE=1;
私有静态字符串注册ID=“”;
@凌驾
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-Intent){
if(requestCode==FILECHOOSER\u RESULTCODE){
if(uploadMessage==null)返回;
Uri result=intent==null | | resultCode!=result_OK?null:intent.getData();
uploadMessage.onReceiveValue(结果);
uploadMessage=null;
}
}
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=(webView)findviewbyd(R.id.container);
WebSettings WebSettings=webView.getSettings();
setJavaScriptEnabled(true);
setWebView.WebChromeClient(新WebChromeClient()
{  
意向i=新意向(意向.行动\u获取\u内容);
@抑制警告(“未使用”)
public void openFileChooser(ValueCallback uploadMsg){
Log.i(“openFileChooser”、“IN”);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),222);
}
@抑制警告(“未使用”)
public void openFileChooser(ValueCallback uploadMsg,String acceptType){
//意向i=新意向(意向.行动\u获取\u内容);
Log.i(“openFileChooser”、“IN”);
i、 addCategory(意图。类别可打开);
i、 集合类型(“*/*”);
startActivityForResult(Intent.createChooser(i,“文件浏览器”),222);
}
public void openFileChooser(ValueCallback uploadMsg、字符串接受类型、字符串捕获){
//openFileChooser(上传消息);
Log.i(“openFileChooser”、“IN”);
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),2223);
}
}); 
webView.getSettings().setJavaScriptEnabled(true);
loadUrl(URL);

}

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

           }

    }); 
webView.setWebChromeClient(新WebChromeClient())
{  
//未记录的magic方法重写
//如果您试图在此处放置@Override,Eclipse将对您发牢骚
//适用于Android 3.0+
public void openFileChooser(ValueCallback uploadMsg){
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),222);
}
//适用于Android 3.0+
public void openFileChooser(ValueCallback uploadMsg,String acceptType){
//意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 集合类型(“*/*”);
startActivityForResult(
Intent.createChooser(i,“文件浏览器”),
222);
}
//适用于Android 4.1
public void openFileChooser(ValueCallback uploadMsg、字符串接受类型、字符串捕获){
//openFileChooser(上传消息);
意向i=新意向(意向.行动\u获取\u内容);
i、 addCategory(意图。类别可打开);
i、 setType(“image/*”);
startActivityForResult(Intent.createChooser(i,“文件选择器”),2223);
}
}); 

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

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

您将在onActivityresult中获得结果,从那里您需要调用webView.uploadfile()之类的函数;我实现了同样的功能。但在4.3或4.4中存在一些特定于版本的问题。它在三星tab 4.1.Hi中工作!请提供最终代码,因为我被困在同一点。。。。谢谢