在webView android中打开图像选择

时间:2016-04-22 07:46:14

标签: java android html web-applications webview

当我在网页视图中点击带有输入类型文件的按钮时,我无法理解为什么我的代码不起作用, 它不会发生任何事情, 我在我的电脑的普通浏览器中检查了html代码,它工作正常,所以问题必须在Activity的代码中  这是MainActivity的完整代码:

public class MainActivity extends Activity {

private String html = "file:///android_asset/program.html";


private WebView mWebView;

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


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

    // Calling async task to get json
    new GetProgram().execute(); //other function

    SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    String json = sharedPref.getString("JSON","");
    Log.i("JSON = ",json);
    editor.commit();


    mWebView = (WebView) findViewById(R.id.webView);
    html = html.concat("?var=");
    html = html.concat(json);
   // mWebView.loadUrl(html);

    mWebView = new WebView(this);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.loadUrl(html);
    mWebView .setWebViewClient(new WebViewClient());
    mWebView .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) {

            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

        }

        // For Android 3.0+
        public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
            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 4.1
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            MainActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);

        }

    });


    setContentView(mWebView);

}

/**
 * .... Here another class
 */


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

谢谢你,如果你能解决我的问题,抱歉我的英语不好,我是意大利人!

program.html文件有一个切换到selfie.html文件的按钮,这是文件:

<div id="container">
    <script>
                            var link = '<form enctype="multipart/form-data"  name="takefoto" method="POST" action="http://www.bestparty.altervista.org/APP/localsite/upload_foto.php?usr=' + usr + '">';
                            document.write(link);
                            </script>
    <div class="upload">


        <input name="uploaded_file" type="file" id="takefoto" /> <br>




    </div>

    <textarea name="comment" cols="100" rows="3" placeholder="Aggiungi una descrizione...">
    </textarea> <br>
    <input type="checkbox" name="anonym" value="yes"/> Pubblica in anonimo  <br>
    <input type="submit" value="Carica foto">
    </form>

</div>

0 个答案:

没有答案