I've been trying to get my first Android app to work. I have one activity that uses a webview, and I use it to open web pages that have html forms on them.
There was some trouble getting the "Choose File" button (for file inputs) to work, but I finally solved it thanks to the help posted here File Upload in WebView. From there, I'm pretty much using the Main Activity java code they made available on Github.
My actual problem is that when clicking a file input button, I don't get the option for the user to use the device's Camera, which I'd like to have. At first I thought it might be related to having to ask for Camera permission for the app, but I implemented it and I was wrong on that one. The problem here is I'm unexperienced with the Intents to get the popup menus, like:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
Some guidance on finding a way to get a "Camera" option to come up would be much appreciated.
Let me show you what I mean by this, opening the same html form on Chrome, and my app on 2 different Android OS versions (4.4.4 and 6.0). Using my Samsung Galaxy Tab, running Android 4.4.4. When opening a page that has an html form, on Google Chrome, clicking the Choose File button, I get this menu
That is what I want to have in my app
GETing using the same URL and displaying it in my App (on 4.4.4), using my webview, when clicking the Choose File button, I get this menu
(Also, I've tried clicking that Choose File button on my app, on an Android 6.0 emulator, and it goes straight to the gallery, and there's no Camera option there):
This is the relevant part of the code:
//For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUM = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult(Intent.createChooser(i, "Choose an Image please"), MainActivity.FCR);
}
//For Android 5.0+
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams){
if(mUMA != null){
mUMA.onReceiveValue(null);
}
mUMA = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCM);
}catch(IOException ex){
Log.e(TAG, "Image file creation failed", ex);
}
if(photoFile != null){
mCM = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}else{
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if(takePictureIntent != null){
intentArray = new Intent[]{takePictureIntent};
}else{
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, FCR);
return true;
}