I am trying to make an android app to access a simple Sinatra Website I have created. The Website allows the user to upload photos. Just using WebView the following way allows me to browse my phone for files.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://10.0.2.2:4567");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
As I tried to make the app better I found this piece of code that makes the app look(and supposedly work) better.
public class MainActivity extends Activity {
protected WebView myWebView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView= (WebView) findViewById(R.id.webview);
myWebView.loadUrl("http://10.0.2.2:4567");
myWebView.setWebViewClient(new NewWebViewClient());
}
private class NewWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView webview,String url)
{
webview.loadUrl(url);
return true;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
{
myWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The problem is now when I tap the browse button in my web page the browsing menu doesn't appear in my screen.Actually nothing happens. I guess NewWebViewClient creates this problem but how is it possible? Is there a way to enable browsing for files again? Do I have to add it manually as I read that it was necessary in earlier android versions?
I am targeting API 18 but the same problem appears for API 17 too.
---------------------------------EDIT-------------------------------
Still having the same problem. openFileChooser gave solution for older APIs but doesn't work for API 18
myWebView.setWebChromeClient(new WebChromeClient()
{
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Image Chooser");
MainActivity16.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
});
setContentView(myWebView);
}