0

I have seen the solution mentioned here, but I can't figure out, how to make it work with my code. Here is the code (displaying simple page with file upload). My app should compatible with Android 2.3-4.1

package com.myapp.test;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.CookieSyncManager;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    @Override
    public void onBackPressed() {
    }   
    
    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;

        }
    }
    
    
    private WebView webView;
    

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        CookieSyncManager.createInstance(this);
        CookieSyncManager.getInstance().startSync();
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        //webView.getSettings().setUserAgentString("Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) version/4.0.4 Mobile/7B367 Safari/531.21.10");
         webView.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) {
                 
                  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);
           
                 }
          });
        
        webView.loadUrl("http://encodable.com/uploaddemo/");
        webView.setWebViewClient(new MyWebViewClient());
        }
    private class MyWebViewClient extends WebViewClient {
         @Override
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }

    }
    
    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/*");
            MainActivity.this.startActivityForResult(
                    Intent.createChooser(i, "Image Browser"),
                    FILECHOOSER_RESULTCODE);
        }

}
    
}

I am not able to get the filechooser to open.

Any ideas? I am kind of hopeless, I am doing something wrong...

Community
  • 1
  • 1
Kokesh
  • 3,165
  • 7
  • 29
  • 46
  • The top-voted answer on the page that you linked to uses a subclass of `WebChromeClient` (not Web*View*Client), which in turn implements `openFileChooser()`. Have you tried doing that? – acj Oct 23 '12 at 13:43
  • yes, I did, but I got lost in the code and never made it working. – Kokesh Oct 23 '12 at 15:22
  • Using `WebChromeClient` is the most straightforward solution that I know of. Maybe try it again and edit this question (with fresh code) if you run into problems? – acj Oct 23 '12 at 15:25
  • I've edited the code. I think, that I did some nonsense in it... – Kokesh Oct 23 '12 at 15:57
  • 1
    The code looks good so far. As currently written, though, it probably works on 2.x. Please see Michel Olivier's answer on the link that you posted, and then look at stalepretzel's answer. Their suggestions, used together, should help you to make the file chooser work on 2.3 through 4.1. – acj Oct 23 '12 at 16:57

0 Answers0