4

Possible Duplicate:
File Upload in WebView

I hope there is an easy answer for this question. I have this Android code running in my app:

package com.example.myfirstapp;

import android.webkit.WebViewClient;
import android.webkit.WebView;
import android.util.Log;

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            Log.d ("myfirstapp_tag", "shouldOver... loaded Url " + url  );
            return true;
    }
}

... but when this HTML is loaded ...

<tr><td>
    <form method="post" action="/myphp.php" enctype="multipart/form-data">
            <label for="upload">Add Pic:</label><input type="file" name="file" id="file" />
            <input type="hidden" name="action" value="upload" />
            <input type="submit" name="submit" value="Upload" />
    </form>
</td></tr>

... the form gets displayed with a "Choose file" button, but when I click on it nothing happens. What am I doing wrong here?

Thanks

Community
  • 1
  • 1
Red Cricket
  • 9,762
  • 21
  • 81
  • 166

1 Answers1

2

I found an answer here on stackoverflow but lost the link so here's what I had to do ...

In my MainActivity class I do this ...

...
import android.webkit.ValueCallback;
import android.net.Uri;

public class MainActivity extends Activity {
...
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;
    }  
}  

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new MyWebViewClient());

    webView.setWebChromeClient(
        new WebChromeClient() {   
            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {  
                mUploadMessage = uploadMsg;  
Log.d ("myfirstapp_tag", "1) MainActivity.java loaded openFileChooser" );
                Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
Log.d ("myfirstapp_tag", "2) MainActivity.java loaded openFileChooser" );                   
                i.addCategory(Intent.CATEGORY_OPENABLE);  
Log.d ("myfirstapp_tag", "3) MainActivity.java loaded openFileChooser" );
                i.setType("image/*");  
Log.d ("myfirstapp_tag", "4) MainActivity.java loaded openFileChooser" );
MainActivity.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);
Log.d ("myfirstapp_tag", "5) MainActivity.java loaded openFileChooser" );                     
            }  
        }
    );  



    webView.loadUrl("http://example.com/EZC.php");
    setContentView(webView);
    Log.d ("myfirstapp_tag", "MainActivity.java loaded URL and set Content View." ); 
}   
 }
Red Cricket
  • 9,762
  • 21
  • 81
  • 166