WebViewClient and fileChooser

主宰稳场 提交于 2019-12-11 00:17:28

问题


I have a problem.. :(

I have a custom webView implemented myself like an extends WebViewClient. In the html side of the application I have a tag <input type='file' id='media' name='media' accept="image/*" capture="camera" /> but I can not make it works!

I follow a lot of tutorial but all of them use WebChromeclient; in my case, because the complexity of the application, I use a WebViewClient.

Is there a solution? Please..

Here there is my class

public class MyWebView extends WebViewClient{
private static Context context;
private static HttpClient client;
private static SharedPreferences prefs;

public MyWebView(Context context, HttpClient client, SharedPreferences prefs){
    super();    
    MyWebView.context = context;
    MyWebView.client = client;
    MyWebView.prefs = prefs;
}


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {                                     
    //some operation with cookie
}

回答1:


I was having the same issue for webview. Let suppose ur html looks like

<html>

<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

here is the whole which solved my problem

package com.example.webviewtest;

public class MainActivity extends Activity {

private WebView wv;

private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    wv = new WebView(this);
    wv.setWebViewClient(new WebViewClient());


    wv.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 uploadMsg) {

            Log.i("For Android < 3.0", "called");

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);


            i.addCategory(Intent.CATEGORY_OPENABLE);

            i.setType("*/*");
            MainActivity.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback uploadMsg,
                String acceptType) {

            Log.i("For Android 3.0+", "called");

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);

            i.addCategory(Intent.CATEGORY_OPENABLE);

            i.setType("*/*");
            MainActivity.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);
        }

         public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
             openFileChooser(uploadMsg);

             Log.i("For Android Jellybeans", "called");

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);

            i.addCategory(Intent.CATEGORY_OPENABLE);

            i.setType("*/*");
            MainActivity.this.startActivityForResult(
                    Intent.createChooser(i, "File Browser"),
                    FILECHOOSER_RESULTCODE);

         } 

    });

    setContentView(wv);
    wv.loadUrl("http://blue.genetechz.com/qadir/");
}

@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {

    Log.i("onActivityResult", "called");

    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (null == mUploadMessage){

            Log.i("if", "return called");
            return;
        }else{

            Uri result = intent == null || resultCode != RESULT_OK ? null
                    : intent.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;
            Log.i("else", "inner Called");

        }

    } else {

        Log.i("Else", "Called");

        // super.onActivityResult(requestCode, resultCode, intent);
        // IPlugin callback = this.activityResultCallback;
        // if (callback != null) {
        // callback.onActivityResult(requestCode, resultCode, intent);
        // }
    }
}

@Override
public void onBackPressed() {
    if (wv.canGoBack() == true) {
        wv.goBack();
    } else {
        MainActivity.this.finish();
    }
}

}


来源:https://stackoverflow.com/questions/19687282/webviewclient-and-filechooser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!