0

This question is encountered several times, though there doesn't seem to be no explanation that works. (or maybe I didn't find it in this chaos called internet)...

I am developing an android app that opens a HTML page that contains an upload button. It doesn't work in WebView.

I have tried: http://m0s-programming.blogspot.in/2011/02/file-upload-in-through-webview-on.html

but eclipse gives warning that "openFileChooser(ValueCallback uploadMsg) is never used locally". The app should work with Android 2.2 (API 8) and above.

It give some errors, I guess due to wrong placement of WebView.setWebChromeClient(new CustomWebChromeClient()

Can someone help me on this?

Chirag
  • 1,189
  • 2
  • 21
  • 43
  • 1
    Read carefully the article: `4) The warning about openFileChooser never used locally is normal, in fact I'm pretty sure it is supposed to give you the warning. If it's not working out for you the problem is somewhere else.` – vortexwolf Mar 16 '13 at 10:26
  • @vorrtex It does give me that warning. But even neglecting that, the app actually crashes on my emulator. – Chirag Mar 16 '13 at 10:36
  • 1
    I found a sample application in comments to the article, and after some small changes it worked. https://dl.dropbox.com/u/8047386/file-attach-cordova-upload-jqm-master_fixed.zip. I'm sure you have something different in your app that crushes whereas uploading works fine. – vortexwolf Mar 16 '13 at 11:06
  • Okay I will try this, thanks. One more thing, will it support all API above 8? – Chirag Mar 16 '13 at 11:41
  • 1
    You will need to add several functions with different signatures but with the same body. Look at this question http://stackoverflow.com/questions/10953957/webview-android-4-0-file-upload – vortexwolf Mar 16 '13 at 11:56
  • @vorrtex seems, there is a different implemetation for different android versions... All that I need is given here: http://stackoverflow.com/questions/5907369/file-upload-in-webview Thanks for all the help. I will let you know about the progress! – Chirag Mar 16 '13 at 13:38
  • I've put up the code above. Getting few errors though. – Chirag Mar 16 '13 at 15:18
  • 1
    If you have errors with the example which I posted, than it is strange, because it worked on my emulator. It should post the file to the test site and display a response from the server. – vortexwolf Mar 16 '13 at 17:55
  • Hey it's working like gem! – Chirag Mar 16 '13 at 18:34
  • Thanks @vorrtex I really appreciate your patience :) – Chirag Mar 16 '13 at 18:36
  • Ok, I posted all links and code as an answer. – vortexwolf Mar 16 '13 at 20:02

2 Answers2

5

A similar question about file upload was answered here: File Upload in WebView.

Also different versions of Android require different methods: https://stackoverflow.com/posts/12746435/edit

Here is full and self-sufficient code of the activity:

public class FileAttachmentActivity extends Activity {

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

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

        WebView 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  
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 3.x
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 4.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                FileAttachmentActivity.this.showAttachmentDialog(uploadMsg);
            }
        });

        this.setContentView(wv);

        wv.loadUrl("https://dl.dropbox.com/u/8047386/posttest.htm");

    }

    private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
        this.mUploadMessage = uploadMsg;

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");

        this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == this.mUploadMessage) {
                return;
            }
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            this.mUploadMessage.onReceiveValue(result);
            this.mUploadMessage = null;
        }
    }
}
Community
  • 1
  • 1
vortexwolf
  • 13,967
  • 2
  • 54
  • 72
  • @jcesar The WebChromeClient class wasn't changed in 4.4, so it should continue working. But I can't verify it now and I have no idea what error you have. – vortexwolf Nov 11 '13 at 11:21
  • No error at all, the button just do nothing, with no log or error trace. – jcesarmobile Nov 11 '13 at 11:28
  • @jcesar They changed the WebView engine, this must have broken the existing code. But you can try to change `targetSdkversion` parameter to 18 and it will work as before: http://developer.android.com/guide/webapps/migrating.html – vortexwolf Nov 11 '13 at 12:35
  • I need this to work so that I can get back to the streamlined world of .NET/C#. – The Muffin Man Nov 12 '14 at 20:38
-2

some devices upload button not active to use like Samsung s4 and note 3