Xamarin Android Web-view File Chooser - android

I wrote a hybrid app and I want to use file upload HTML (file and camera) page. I use the below code and it works well.
When the user clicks on FileUploader input, the native window opens (to choose between File or Camera) and everything works just fine.
My only problem is that, when user click out of the window, this one will be closed and after no more opened, maybe something to reinitialise or restart.
If someone has some ideas...
On MainActivity:
var chrome = new SmarterWebChromeClient((uploadMsg) =>
{
mUploadMessage = uploadMsg;
mCameraPhotoPath = null;
Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
//Create the File where the photo should go
File photoFile = null;
try
{
photoFile = createImageFile();
takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
}
catch (IOException ex)
{
// Error occurred while creating the File
writeEx(ex.ToString());
}
// Continue only if the File was successfully created
if (photoFile != null)
{
mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput,
Android.Net.Uri.FromFile(photoFile));
}
else
{
takePictureIntent = null;
}
Intent contentSelectionIntent = new Intent(Intent.ActionGetContent);
contentSelectionIntent.AddCategory(Intent.CategoryOpenable);
contentSelectionIntent.SetType("image/*");
Intent[] intentArray;
if (takePictureIntent != null)
{
intentArray = new Intent[] { takePictureIntent };
}
else
{
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ActionChooser);
chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
chooserIntent.PutExtra(Intent.ExtraTitle, "Choisir une photo");
chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);
StartActivityForResult(chooserIntent, MainActivity.FILECHOOSER_RESULTCODE);
});
mWebView.SetWebViewClient(new MyWebViewClient());
mWebView.SetWebChromeClient(chrome);
...
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
if (requestCode == FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage)
return;
// Check that the response is a good one
if (resultCode == Result.Ok)
{
Android.Net.Uri[] results = null;
if (intent == null)
{
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(mCameraPhotoPath) };
}
else
{
}
}
else
{
if (intent.DataString != null)
{
results = new Android.Net.Uri[] { Android.Net.Uri.Parse(intent.DataString) };
}
}
mUploadMessage.OnReceiveValue(results);
mUploadMessage = null;
}
}
}
And my WebChromeClient Class :
partial class SmarterWebChromeClient : WebChromeClient
{
Action<IValueCallback> callback;
public SmarterWebChromeClient(Action<IValueCallback> callback)
{
this.callback = callback;
}
public override bool OnShowFileChooser(WebView webView, IValueCallback filePathCallback, FileChooserParams fileChooserParams)
{
callback(filePathCallback);
return true;
}
}

I solve your problem like below code
Put an else part for result NOT OK
// Check that the response is a good one
if (resultCode == Result.Ok)
{
//...
}
else
{
mUploadMessage.OnReceiveValue(null);
mUploadMessage = null;
}

Related

Camera Crashing on Android 11 with IllegalStateException

I am opening the camera and file chooser in my application. It's working fine in all versions including android 11. But in google pixel 5 with android 11, it's breaking while trying to open the camera. App not breaking but camera keep closing alert is showing and below exception is happening.
java.lang.IllegalStateException: GCA not granted write permission to file:///storage/emulated/0/Pictures/JPEG_20210727_095347_634440296695227002.jpg
Java code :
/***Camera or file chooser ***/
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
if ( Build.VERSION.SDK_INT <= 29) {
file_path = filePathCallback;
Intent takePictureIntent = null;
boolean includePhoto = true;
if (includePhoto)
{
takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
File photoFile = null;
try {
photoFile = create_image();
takePictureIntent.putExtra("PhotoPath", cam_file_data);
} catch (IOException ex) {
Log.e(TAG, "Image file creation failed", ex);
}
if (photoFile != null) {
cam_file_data = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
} else {
cam_file_data = null;
takePictureIntent = null;
}
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType(file_type);
if (multiple_files) {
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
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, "File chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, file_req_code);
return true;
} else if ( Build.VERSION.SDK_INT >= 30) {
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
}
if (photoFile != null) {
mCameraPhotoPath = "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, INPUT_FILE_REQUEST_CODE);
return true;
} else {
return false;
}
}
private File create_image() throws IOException {
#SuppressLint("SimpleDateFormat")
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "img_" + timeStamp + "_";
File storageDir = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES); //getCacheDir();
return File.createTempFile(imageFileName, ".jpg", storageDir);
}
Call back
/*******Camera Request Callback******/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null)
{
super.onActivityResult(requestCode, resultCode, intent);
return;
}
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (intent == null) {
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = intent.getDataString();
if (dataString != null)
{
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.Q) {
Uri[] results = null;
if (resultCode == Activity.RESULT_CANCELED) {
if (requestCode == file_req_code) {
file_path.onReceiveValue(null);
return;
}
}
if (resultCode == Activity.RESULT_OK) {
if (requestCode == file_req_code) {
if (null == file_path) {
return;
}
ClipData clipData;
String stringData;
try {
clipData = intent.getClipData();
stringData = intent.getDataString();
} catch (Exception e) {
clipData = null;
stringData = null;
}
if (clipData == null && stringData == null && cam_file_data != null) {
results = new Uri[]{
Uri.parse(cam_file_data)
};
} else {
if (clipData != null) {
final int numSelectedFiles = clipData.getItemCount();
results = new Uri[numSelectedFiles];
for (int i = 0; i < clipData.getItemCount(); i++) {
results[i] = clipData.getItemAt(i).getUri();
}
} else {
results = new Uri[]{
Uri.parse(stringData)
};
}
}
}
}
file_path.onReceiveValue(results);
file_path = null;
} else {
if (requestCode == file_req_code) {
if (null == file_data)
return;
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
file_data.onReceiveValue(result);
file_data = null;
}
}
}
Android manifest file :
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAMERA2" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="29"
/>

Web view displaying small crashed image icon after I click yes icon on camera intent screen

I am creating an app to load an URL in web view. I have created Image chooser to start activity for choose the photo from gallery and camera.
A) Select image from gallery is working perfectly.
B) When I capture image through camera then It is showing crashed small image icon in web view.
Note : Camera photo is displaying perfectly on web view when I stay more then 5-8 seconds on Camera screen after image captured. But It should be work even I press YES button quickly after captured the image.
Here is my web view Code for set WebChromeClient
webView.setWebChromeClient(new WebChromeClient() {
#Override
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> newFilePathCallback,
FileChooserParams fileChooserParams) {
if (filePathCallback != null) {
filePathCallback.onReceiveValue(null);
}
filePathCallback = newFilePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
Log.d("Mainn", "onShowFileChooser()");
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
Log.d("Mainn", "onShowFileChooser().. e : " + e.getLocalizedMessage());
e.printStackTrace();
}
Log.d("Mainn", "onShowFileChooser() 4");
// Continue only if the File was successfully created
if (photoFile != null) {
Log.d("Mainn", "onShowFileChooser() 5");
cameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
Log.d("Mainn", "onShowFileChooser() 6");
takePictureIntent = null;
}
}
Log.d("Mainn", "onShowFileChooser() 7");
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
Log.d("Mainn", "onShowFileChooser() 8");
intentArray = new Intent[]{takePictureIntent};
} else {
Log.d("Mainn", "onShowFileChooser() 9");
intentArray = new Intent[0];
}
Log.d("Mainn", "onShowFileChooser() 10");
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, INPUT_FILE_REQUEST_CODE);
return true;
}
});
}
onActivityResult() Code is here
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
if (requestCode != INPUT_FILE_REQUEST_CODE || filePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
// If there is not data, then we may have taken a photo
if (cameraPhotoPath != null) {
results = new Uri[]{Uri.parse(cameraPhotoPath)};
}
} else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
filePathCallback.onReceiveValue(results);
filePathCallback = null;
}
Use FileProvider instead of Uri.fromFile(). It will solve this problem.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = FileProvider.getUriForFile(getActivity(), getActivity().getPackageName(), photoFile);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

Photo upload from Camera not working in Nexus, Android WebView

I am working on android webview. I am facing problem while uploading the image from Camera action in Nexus 5. Here, Camera get open, I click the image but then nothing happens,image is not get upload. But it works fine with like Document or Gallery. There are a lot solution and this. But I am unable to solve my problem. What can be done to solve this issue?
In my menifest file, I include :
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
In my activity, i include :
private static final int INPUT_FILE_REQUEST_CODE = 1;
private static final int FILECHOOSER_RESULTCODE = 1;
private static final String TAG = MainActivity.class.getSimpleName();
private WebView webView;
private ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageURI = null;
private ValueCallback<Uri[]> mFilePathCallback;
private String mCameraPhotoPath;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (requestCode != INPUT_FILE_REQUEST_CODE || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
}
else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
} else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
if (requestCode != FILECHOOSER_RESULTCODE || mUploadMessage == null) {
super.onActivityResult(requestCode, resultCode, data);
return;
}
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
result = data == null ? mCapturedImageURI : data.getData();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e,
Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
return;
}
For my WebChromeClient, I include :
public class MyWebChromeClient extends WebChromeClient{
// For Android 5.0
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePath;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Unable to create Image File", ex);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "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, INPUT_FILE_REQUEST_CODE);
return true;
}
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
// Create AndroidExampleFolder at sdcard
// Create AndroidExampleFolder at sdcard
File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "Photo Folder");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
Log.d("File", "File: " + file);
mCapturedImageURI = Uri.fromFile(file);
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[] { captureIntent });
// On select image call onActivityResult method of activity
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
//openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType,
String capture) {
openFileChooser(uploadMsg, acceptType);
}
}
What can be done here, so that image get uploaded via Camera action ?
I solve my issue for Camera by changing following line of code in onActivityResult method :
if (resultCode == Activity.RESULT_OK) {
if (data == null || data.getDataString() == null) {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = data.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
I add this : if (data == null || data.getDataString() == null) {...}
Add android:requestLegacyExternalStorage="true" to the application tag in manifest

File Chooser not opening in Android Lollipop on Oppo F1 & LG G2

I have one problem with Oppo F1 and LG G2 running Lollipop. On other devices running Lollipop, the problem does not appear
My Problem is that File Chooser doesn't show with the mentioned devices.
What I'm currently doing:
private WebView web;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_event);
web = (WebView) findViewById(R.id.webView_addEvent);
web.loadUrl(tempURL);
startWebViewForLolipop();
}
private void startWebViewForLolipop() {
//CODING FOR LOLIPOP VERSION
// Create new webview Client to show progress dialog
// Called When opening a url or click on link
// You can create external class extends with WebViewClient
// Taking WebViewClient as inner class
web.setWebViewClient(new WebViewClient() {
// ProgressDialog progressDialog;
//If you will not use this method url links are open in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// Check if Url contains ExternalLinks string in url
// then open url in new browser
// else all webview links will open in webview browser
if (url.contains("google")) {
// Could be cleverer and use a regex
//Open links in new browser
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// Here we can open new activity
return true;
} else {
// Stay within this webview and load url
view.loadUrl(url);
return true;
}
}
//Show loader on url load
public void onLoadResource(WebView view, String url) {
// if url contains string androidexample
// Then show progress Dialog
/* if (progressDialog == null *//*&& url.contains("androidexample")*//*
) {
// in standard case YourActivity.this
progressDialog = new ProgressDialog(AddEvent.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}*/
}
// Called when all page resources loaded
public void onPageFinished(WebView view, String url) {
try {
// Close progressDialog
if (probressbar.isShown())
probressbar.setVisibility(View.GONE);
} catch (Exception exception) {
exception.printStackTrace();
}
}
});
// You can create external class extends with WebChromeClient
// Taking WebViewClient as inner class
// we will define openFileChooser for select file from camera or sdcard
web.setWebChromeClient(new WebChromeClient() {
// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
// Update message
mUploadMessage = uploadMsg;
try {
// Create ImgDir Folder at sdcard
File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "ImgDir");
if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
} else {
//Delete Old Content
if (imageStorageDir.isDirectory()) {
String[] children = imageStorageDir.list();
for (int i = 0; i < children.length; i++) {
new File(imageStorageDir, children[i]).delete();
}
}
imageStorageDir.delete();
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mCapturedImageURI = Uri.fromFile(file);
// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");
// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[]{captureIntent});
// On select image call onActivityResult method of activity
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
} catch (Exception e) {
Toast.makeText(getBaseContext(), "Exception:" + e,
Toast.LENGTH_LONG).show();
}
}
// openFileChooser for Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooser(uploadMsg, "");
}
//openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType,
String capture) {
openFileChooser(uploadMsg, acceptType);
}
// The webPage has 2 filechoosers and will send a
// console message informing what action to perform,
// taking a photo or updating the file
public boolean onConsoleMessage(ConsoleMessage cm) {
onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
return true;
}
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
Log.d("androidruntime", "Show console messages, Used for debugging: " + message);
}
}); // End setWebChromeClient
/*
web.setWebChromeClient(new WebChromeClient() {
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
*/
/* File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "ImgDir/addEvent");
if (!imageStorageDir.exists()) {
// Create ImgDir/addEvent Folder at sdcard
imageStorageDir.mkdirs();
}
// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");
mCapturedImageURI = Uri.fromFile(file);*//*
*/
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mFilePathCallback = filePathCallback;
try {
Intent intent = fileChooserParams.createIntent();
startActivityForResult(intent, 1);
} catch (Exception e) {
// TODO: when open file chooser failed
}
} else {*//*
// Set up the take picture intent
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(AddEvent.this.getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException e){
e.printStackTrace();
Log.e("TAG", "Unable to create Image File", e);
}
// Continue only if the File was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
// Set up the intent to get an existing image
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image*/
/*");
// Set up the intents for the Intent chooser
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, 1);
// }
return true;
}
});
*/
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == this.mUploadMessage) {
return;
}
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
//result = intent == null ? mCapturedImageURI : intent.getData();
Log.d("TAG", "Image File Path : " + mCapturedImageURI);
String test = intent.toString();
if (intent == null) {
String temp = decodeFile(mCapturedImageURI.getPath(), getImageName(mCapturedImageURI.getPath()));
result = Uri.parse("file:///"+temp);
//result = mCapturedImageURI;
} else {
if (test.equals("Intent { }")) {
String temp = decodeFile(mCapturedImageURI.getPath(), getImageName(mCapturedImageURI.getPath()));
result = Uri.parse("file:///"+temp);
//result = mCapturedImageURI;
} else {
String temp = decodeFile(intent.getData().toString(), getImageName(intent.getData().toString()));
result = Uri.parse("file:///"+temp);
result = Uri.parse(decodeFile(intent.getData().toString(), getImageName(intent.getData().toString())));
//result = intent.getData();
}
}
/* // retrieve from the private variable if the intent is null
String test = intent.toString();
if (intent == null) {
result = mCapturedImageURI;
} else {
if (test.equals("Intent { }")) {
result = mCapturedImageURI;
} else
result = intent.getData();
}*/
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e,
Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
} else {
/* if (requestCode == 1 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mFilePathCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mFilePathCallback = null;
return;
} else {*/
if (requestCode == FILECHOOSER_RESULTCODE) {
//// TODO: 23/6/16 temporary comment this code
/*if (requestCode != 1 || mFilePathCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}*/
if (null == this.mUploadMessage) {
return;
}
Uri result = null;
try {
if (resultCode != RESULT_OK) {
result = null;
} else {
// retrieve from the private variable if the intent is null
//result = intent == null ? mCapturedImageURI : intent.getData();
Log.d("TAG", "Image File Path : " + mCapturedImageURI);
if (intent == null) {
String temp = decodeFile(mCapturedImageURI.getPath(), getImageName(mCapturedImageURI.getPath()));
result = Uri.parse("file:///"+temp);
//result = mCapturedImageURI;
} else {
String test = intent.toString();
if (test.equals("Intent { }")) {
String temp = decodeFile(mCapturedImageURI.getPath(), getImageName(mCapturedImageURI.getPath()));
result = Uri.parse("file:///"+temp);
//result = mCapturedImageURI;
} else {
String temp = decodeFile(intent.getData().toString(), getImageName(intent.getData().toString()));
result = Uri.parse("file:///"+temp);
result = Uri.parse(decodeFile(intent.getData().toString(), getImageName(intent.getData().toString())));
//result = intent.getData();
}
}
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "activity :" + e, Toast.LENGTH_LONG).show();
}
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
/* Uri[] results = null;
// Check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (intent == null) {
// If there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = intent.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
mFilePathCallback.onReceiveValue(results);
mFilePathCallback = null;
return;*/
// }
}
}
}

onShowFileChooser() from android webview works only once

I need to pick images from the device and upload it to the server. For the first time, when I pick the images, onShowFileChooser() gets called and everything works. But, when I try to click upload again, onShowFileChooser() never gets called. But it's working for non-lollypop devices. openFileChoser() gets called, whenever I click upload. Is there anything that I'm missing. Here is my code :
//Needed for file upload feature
vWebView.setWebChromeClient(new WebChromeClient() {
// file upload callback (Android 2.2 (API level 8) -- Android 2.3 (API level 10)) (hidden method)
public void openFileChooser(ValueCallback<Uri> filePathCallback) {
showAttachmentDialog(filePathCallback);
}
// file upload callback (Android 3.0 (API level 11) -- Android 4.0 (API level 15)) (hidden method)
public void openFileChooser(ValueCallback filePathCallback, String acceptType) {
showAttachmentDialog(filePathCallback);
}
// file upload callback (Android 4.1 (API level 16) -- Android 4.3 (API level 18)) (hidden method)
public void openFileChooser(ValueCallback<Uri> filePathCallback, String acceptType, String capture) {
showAttachmentDialog(filePathCallback);
}
// file upload callback (Android 5.0 (API level 21) -- current) (public method)
// for Lollipop, all in one
#Override
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mFilePathCallbackArray != null) {
mFilePathCallbackArray.onReceiveValue(null);
}
mFilePathCallbackArray = filePathCallback;
// Set up the take picture intent
if (mTypeCap == IMAGE) {
Intent takePictureIntent = pictureIntentSetup();
return showChooserDialog(takePictureIntent);
}
//set up video capture intent
else {
Intent takeVideoIntent = videoIntentSetUp();
return showChooserDialog(takeVideoIntent);
}
}
});
//For lollypop
private Intent pictureIntentSetup() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// create the file where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("Failed", "Unable to create Image File", ex);
}
// continue only if the file was successfully created
if (photoFile != null) {
mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
} else {
takePictureIntent = null;
}
}
return takePictureIntent;
}
//For lollypop
private Intent videoIntentSetUp() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// create the file where the video should go
File videoFile = null;
try {
videoFile = createVideoFile();
takeVideoIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e("Failed", "Unable to create Video File", ex);
}
// continue only if the file was successfully created
if (videoFile != null) {
mCameraPhotoPath = "file:" + videoFile.getAbsolutePath();
takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(videoFile));
} else {
takeVideoIntent = null;
}
}
return takeVideoIntent;
}
//For lollypop
private boolean showChooserDialog(Intent intent) {
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (mTypeCap.equalsIgnoreCase(IMAGE))
contentSelectionIntent.setType(IMAGE);
else
contentSelectionIntent.setType(VIDEO);
Intent[] intentArray;
if (intent != null) {
intentArray = new Intent[]{intent};
} else {
intentArray = new Intent[0];
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
if (mTypeCap.equalsIgnoreCase(IMAGE))
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
else
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Video Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
getActivity().startActivityForResult(chooserIntent, FILE_CHOOSER_RESULT_CODE);
return true;
}
OnActivityResult of the activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//File upload related
if (requestCode == NewsDetailFragment.FILE_CHOOSER_RESULT_CODE && (resultCode == RESULT_OK || resultCode == RESULT_CANCELED)) {
Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.container);
if (fragment != null && fragment instanceof DetailFragment) {
Fragment currentFragment = ((DetailFragment) fragment).getCurrentFragment();
if (currentFragment instanceof WebDetailFragment)
currentFragment.onActivityResult(requestCode, resultCode, data);
}
}
}
}
onActivityResult of fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intentData) {
super.onActivityResult(requestCode, resultCode, intentData);
// code for all versions except of Lollipop
if (!Utility.isLollypopAndAbove()) {
Uri result = null;
// check that the response is a good one
if (resultCode == Activity.RESULT_OK) {
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (null == this.mFilePathCallback) {
return;
}
if (null == mFilePathCallback) return;
if (intentData == null) {
// if there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
result = Uri.parse(mCameraPhotoPath);
}
} else {
String dataString = intentData.getDataString();
if (dataString != null) {
result = Uri.parse(dataString);
}
}
// Uri result = intentData == null || resultCode != Activity.RESULT_OK ? null
// : intentData.getData();
}
// for Lollipop only
}
mFilePathCallback.onReceiveValue(result);
mFilePathCallback = null;
}
else {
Uri[] results = null;
// check that the response is a good one
if(resultCode==Activity.RESULT_OK) {
if (requestCode == FILE_CHOOSER_RESULT_CODE) {
if (null == mFilePathCallbackArray) {
return;
}
if (intentData == null) {
// if there is not data, then we may have taken a photo
if (mCameraPhotoPath != null) {
results = new Uri[]{Uri.parse(mCameraPhotoPath)};
}
} else {
String dataString = intentData.getDataString();
if (dataString != null) {
results = new Uri[]{Uri.parse(dataString)};
}
}
}
}
mFilePathCallbackArray.onReceiveValue(results);
mFilePathCallbackArray = null;
return;
}
}
firstly, sorry to my english. you should return empty Uri[]{} to file receive
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
my code can choose take photo or local image:
private static final int REQUEST_GET_THE_THUMBNAIL = 4000;
private static final long ANIMATION_DURATION = 200;
public final static int FILECHOOSER_RESULTCODE = 1;
public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
//JS
webView.getSettings().setJavaScriptEnabled(true);
//set ChromeClient
webView.setWebChromeClient(getChromeClient());
//ChromeClinet配置
private WebChromeClient getChromeClient() {
return new WebChromeClient() {
//3.0++
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
openFileChooserImpl(uploadMsg);
}
//3.0--
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
openFileChooserImpl(uploadMsg);
}
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
openFileChooserImpl(uploadMsg);
}
// For Android > 5.0
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
openFileChooserImplForAndroid5(uploadMsg);
return true;
}
};
}
private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
new AlertDialog.Builder(mActivity)
.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (items[which].equals(items[0])) {
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
} else if (items[which].equals(items[1])) {
dispatchTakePictureIntent();
}
dialog.dismiss();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.v(TAG, TAG + " # onCancel");
mUploadMessage = null;
dialog.dismiss();
}})
.show();
}
private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {
mUploadMessageForAndroid5 = uploadMsg;
new AlertDialog.Builder(mActivity)
.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (items[which].equals(items[0])) {
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
} else if (items[which].equals(items[1])) {
dispatchTakePictureIntent();
}
dialog.dismiss();
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.v(TAG, TAG + " # onCancel");
//important to return new Uri[]{}, when nothing to do. This can slove input file wrok for once.
//InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
mUploadMessageForAndroid5 = null;
dialog.dismiss();
}}).show();
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(mActivity.getPackageManager()) != null) {
// File file = new File(createImageFile());
Uri imageUri = null;
try {
imageUri = Uri.fromFile(createImageFile());
} catch (IOException e) {
e.printStackTrace();
}
//temp sd card file
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(takePictureIntent, REQUEST_GET_THE_THUMBNAIL);
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/don_test/");
if (!storageDir.exists()) {
storageDir.mkdirs();
}
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Log.v(TAG, TAG + " # onActivityResult # requestCode=" + requestCode + " # resultCode=" + resultCode);
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != Activity.RESULT_OK ? null : intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
} else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5) {
if (null == mUploadMessageForAndroid5)
return;
Uri result;
if (intent == null || resultCode != Activity.RESULT_OK) {
result = null;
} else {
result = intent.getData();
}
if (result != null) {
Log.v(TAG, TAG + " # result.getPath()=" + result.getPath());
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
} else {
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
}
mUploadMessageForAndroid5 = null;
} else if (requestCode == REQUEST_GET_THE_THUMBNAIL) {
if (resultCode == Activity.RESULT_OK) {
File file = new File(mCurrentPhotoPath);
Uri localUri = Uri.fromFile(file);
Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri);
mActivity.sendBroadcast(localIntent);
Uri result = Uri.fromFile(file);
mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
mUploadMessageForAndroid5 = null;
} else {
File file = new File(mCurrentPhotoPath);
Log.v(TAG, TAG + " # file=" + file.exists());
if (file.exists()) {
file.delete();
}
}
}
}
Faced similar problem i was returning boolean value in method onShowFileChooser, issue was fixed when i called super class method
#Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
//Logic to implement
return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
}
In the onShowFileChooser() method you should return true, only if you are using the filePathCallback, which is the best way:
private ValueCallback<Uri[]> filePathCallback;
#Override
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
// do whatever you need to do, to show a file chooser/camera intent
this.filePathCallback = filePathCallback;
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// Check for your correct requestCode from your intent here
if (resultCode == RESULT_CANCELED) {
// this is important, call the callback with null parameter
this.filePathCallback.onReceiveValue(null);
} else if (resultCode == RESULT_OK) {
// extract the uri(s) you need here
this.filePathCallback.onReceiveValue(new Uri[]{result});
}
}
I had the similar issue that onShowFileChooser only works once. After few hours of trail and error and then I figured out using a simple experiment:
Not invoking ValueCallback will result onShowFileChooser only works once. Below code only works once...
override fun onShowFileChooser(
view: WebView?,
filePath: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
Log.i("blah", "<== onShowFileChooser ==>")
// filePath?.onReceiveValue(null)
return true
}
Properly invoking ValueCallback will make onShowFileChooser work every time:
override fun onShowFileChooser(
view: WebView?,
filePath: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?
): Boolean {
Log.i("blah", "<== onShowFileChooser ==>")
filePath?.onReceiveValue(null)
return true
}
so I just check all branches and make sure all properly invoking ValueCallback then the webview works as expected.
I had the same issue, the problem was I was expecting an OnActivityResult, but when you press the back button, this was not triggered.
The solution was implementing on onResume the following code, to tell the callback the answer was empty and being able to reuse it:
#Override
protected void onResume() {
super.onResume();
if (mFilePathCallbackArray == null)
return;
mFilePathCallbackArray.onReceiveValue(new Uri[]{});
mFilePathCallbackArray = null;
}
Just set a null value:
if (resultCode == Activity.RESULT_OK) {
handleImageRequest(data)
} else {
mValueCallbackArray?.onReceiveValue(null)
mValueCallbackArray = null
}
I had exactly same issue, But my use case is little different but my webview was loaded on a fragment as is in your code. first time the file chooser displayed fine on both Lollipop & pre Lollipop devices, but next time onwards it failed to display file chooser it self. The Reason being the I was not handled the onActivityResult in proper way at first time. My working code snippet is as follows for Lollipop:
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
private boolean onActivityResultLolliPop(
int requestCode, int resultCode, Intent data) {
if (requestCode != SurveyWebChromeClient.FILE_CHOOSER_REQUEST_CODE
|| mFilePathCallbackL == null) {
super.onActivityResult(requestCode, resultCode, data);
return true;
}
Uri[] results = null;
if (resultCode == Activity.RESULT_OK) {
if (data == null) {
if (mCameraPhotoVideoPath != null) {//if there is not data here, then we may have taken a photo/video
results = new Uri[]{Uri.parse(mCameraPhotoVideoPath)};
}
} else {
String dataString = data.getDataString();
ClipData clipData = data.getClipData();
if (clipData != null) {
results = new Uri[clipData.getItemCount()];
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
results[i] = item.getUri();
}
}
if (dataString != null)
results = new Uri[]{Uri.parse(dataString)};
}
}
mFilePathCallbackL.onReceiveValue(results);
mFilePathCallbackL = null;
return false;
}
Just let onShowFIleChooser() return false, will work next time
public boolean onShowFileChooser(){
...
return **false**;
}
This is working for me (Kotlin, Jetpack compose)->
override fun onShowFileChooser(
webView: WebView?,
filePathCb: ValueCallback<Array<Uri>>?,
fileChooserParams: FileChooserParams?,
): Boolean {
if (filePathCallback != null) {
filePathCallback!!.onReceiveValue(null)
}
filePathCallback = filePathCb
return true
}
In future I can upload or reject the file upload->
if(onCloseCameraClick){
filePathCallback?.onReceiveValue(null)
filePathCallback = null
showMediaPicker = false
}
else{
filePathCallback?.onReceiveValue(arrayOf(uri))
filePathCallback = null
showMediaPicker = false
}

Categories

Resources