File Upload in WebView

后端 未结 19 1786
旧巷少年郎
旧巷少年郎 2020-11-22 00:28

I have been struggling to upload files from WebView since last few days and there is no progress. I googled and implemented all suggested solutions but none works, like: sol

相关标签:
19条回答
  • 2020-11-22 00:44

    I found that I needed 3 interface definitions in order to handle various version of android.

    public void openFileChooser(ValueCallback < Uri > uploadMsg) {
      mUploadMessage = uploadMsg;
      Intent i = new Intent(Intent.ACTION_GET_CONTENT);
      i.addCategory(Intent.CATEGORY_OPENABLE);
      i.setType("image/*");
      FreeHealthTrack.this.startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILECHOOSER_RESULTCODE);
    }
    
    public void openFileChooser(ValueCallback < Uri > uploadMsg, String acceptType) {
      openFileChooser(uploadMsg);
    }
    
    public void openFileChooser(ValueCallback < Uri > uploadMsg, String acceptType, String capture) {
      openFileChooser(uploadMsg);
    }
    
    0 讨论(0)
  • 2020-11-22 00:45

    In KitKat you can use the Storage Access Framework.

    Storage Access Framework / Writing a Client App

    0 讨论(0)
  • 2020-11-22 00:46

    this is the only solution that i found that works!

    WebView webview;
    
    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;
    
        }
    }
    
    // Next part 
    
    class MyWebChromeClient extends WebChromeClient {
        // The undocumented magic method override
        // Eclipse will swear at you if you try to put @Override here
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {
    
            mUploadMessage = uploadMsg;
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            Cv5appActivity.this.startActivityForResult(
                    Intent.createChooser(i, "Image Browser"),
                    FILECHOOSER_RESULTCODE);
        }
    }
    
    0 讨论(0)
  • 2020-11-22 00:50

    This is a full solution for all android versions, I had a hard time with this too.

    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);
    }*/
    }
    

    Also I want to add that the "upload page" like the one in this example, wont work on < 4 versions, since it has an image preview feature, if you want to make it work use a simple php upload without preview.

    Update:

    Please find the solution for lollipop devices here and thanks for gauntface

    Update 2:

    Complete solution for all android devices till oreo here and this is more advanced version, you should look into it, maybe it can help.

    0 讨论(0)
  • 2020-11-22 00:51

    Found a SOLUTION which works for me! Add one more rule in the file proguard-android.txt:

    -keepclassmembers class * extends android.webkit.WebChromeClient {
         public void openFileChooser(...);
    }
    
    0 讨论(0)
  • 2020-11-22 00:51

    I'm new to Andriod and struggled with this also. According to Google Reference Guide WebView.

    By default, a WebView provides no browser-like widgets, does not enable JavaScript and web page errors are ignored. If your goal is only to display some HTML as a part of your UI, this is probably fine; the user won't need to interact with the web page beyond reading it, and the web page won't need to interact with the user. If you actually want a full-blown web browser, then you probably want to invoke the Browser application with a URL Intent rather than show it with a WebView.

    Example code I executed in MainActvity.java.

     Uri uri = Uri.parse("https://www.example.com");
     Intent intent = new Intent(Intent.ACTION_VIEW, uri);
     startActivity(intent);
    

    Excuted

    package example.com.myapp;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.content.Intent;
    import android.net.Uri;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Uri uri = Uri.parse("http://www.example.com/");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
            getSupportActionBar().hide();
        }}
    
    0 讨论(0)
提交回复
热议问题