0

I am making an Android app in WebView and I need to add a code to upload for Android. I have found this code (BELOW) and I need to make that into a JavaScript instead of java (if possible) I need to tie it to this upload button (below) I would like for it to also work from the website as well.

Screenshot:

screenshot

Code:

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);
    }*/
}
ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Nybson
  • 1
  • 1

2 Answers2

0

Check this similar topic that i had found for you.

Topic similar Upload File & Full code

See you soon. Bye bye Markus

Community
  • 1
  • 1
Markus Werner
  • 109
  • 12
0

Just use a jQuery File Upload;

$(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

tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • Unfortunately when I convert it with web view it does not work, no. – Nybson Dec 04 '15 at 15:54
  • So, you may need a javascriptInterface to help you .when you click btn in html, pass the event to the webview.Or did you enable javascript in Webview? – tiny sunlight Dec 05 '15 at 05:18