【问题标题】:|WebVIew|Where to implement script for uploading|WebVIew|上传脚本在哪里实现
【发布时间】:2015-12-04 15:19:03
【问题描述】:

我正在 WebView 中制作一个 Android 应用程序,我需要添加一个代码来为 Android 上传。我找到了这段代码(下面),我需要将它变成 JavaScript 而不是 java(如果可能)我需要将它绑定到这个上传按钮(下面)我希望它也可以在网站上工作。

截图:

代码:

public class MyWb extends Activity {
    /** Called when the activity is first created. */

    WebView web;
    ProgressBar progressBar;

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

     @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;  
      }
      }  

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        web = (WebView) findViewById(R.id.webview01);
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);

        web = new WebView(this);  
        web.getSettings().setJavaScriptEnabled(true);
        web.loadUrl("http://www.script-tutorials.com/demos/199/index.html");
        web.setWebViewClient(new myWebClient());
        web.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/*");  
                MyWb.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("*/*");
               MyWb.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/*");  
                   MyWb.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MyWb.FILECHOOSER_RESULTCODE );

               }

        });  


        setContentView(web);  


    }

    public class myWebClient extends WebViewClient
    {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            // TODO Auto-generated method stub

            view.loadUrl(url);
            return true;

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
            super.onPageFinished(view, url);

            progressBar.setVisibility(View.GONE);
        }
    }

    //flipscreen not loading again
    @Override
    public void onConfigurationChanged(Configuration newConfig){        
        super.onConfigurationChanged(newConfig);
    }

    // To handle "Back" key press event for WebView to go back to previous screen.
    /*@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) 
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
            web.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }*/
}

【问题讨论】:

  • 那你为什么要使用原生文件上传?就像它是普通网页一样。

标签: javascript java android html webview


【解决方案1】:

查看我为您找到的类似主题。

Topic similar Upload File & Full code

很快再见。 再见 马库斯

【讨论】:

    【解决方案2】:

    只需使用 jQuery 文件上传;

    $(function () {
        'use strict';
        // Change this to the location of your server-side upload handler:
        var url = window.location.hostname === 'blueimp.github.io' ?
                    '//jquery-file-upload.appspot.com/' : 'server/php/';
        $('#fileupload').fileupload({
            url: url,
            dataType: 'json',
            done: function (e, data) {
                $.each(data.result.files, function (index, file) {
                    $('<p/>').text(file.name).appendTo('#files');
                });
            },
            progressall: function (e, data) {
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .progress-bar').css(
                    'width',
                    progress + '%'
                );
            }
        }).prop('disabled', !$.support.fileInput)
            .parent().addClass($.support.fileInput ? undefined : 'disabled');
    });
    

    http://www.yyyweb.com/demo/inner-show/jquery-upload.html

    【讨论】:

    • 不幸的是,当我用网络视图转换它时它不起作用,不。
    • 那么,您可能需要一个javascriptInterface来帮助您。当您在html中单击btn时,将事件传递给webview。或者您是否在Webview中启用了javascript?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2012-05-22
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多