下面列出了android.webkit.WebChromeClient#FileChooserParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
if (openFileCallback2 != null) {
openFileCallback2.onReceiveValue(null);
openFileCallback2 = null;
}
openFileCallback2 = filePathCallback;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Intent intent = fileChooserParams.createIntent();
try {
startActivityForResult(intent, REQUEST_FILE_CHOOSER);
} catch (ActivityNotFoundException e) {
openFileCallback2 = null;
return false;
}
}
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
MainActivity.current().getStateListener().launch((resultCode, data)
-> uploadMsg.onReceiveValue(new Uri[]{data.getData()})
, fileChooserParams.createIntent());
return true;
}
private void startFileChooser(WebChromeClient.FileChooserParams fileChooserParams) {
final String[] acceptTypes = getSafeAcceptedTypes(fileChooserParams);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final boolean allowMultiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
Intent intent = fileChooserParams.createIntent();
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_MIME_TYPES, getAcceptedMimeType(acceptTypes));
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, allowMultiple);
getCurrentActivity().startActivityForResult(intent, SELECT_FILE);
}
}
private String[] getSafeAcceptedTypes(WebChromeClient.FileChooserParams params) {
// the getAcceptTypes() is available only in api 21+
// for lower level, we ignore it
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return params.getAcceptTypes();
}
final String[] EMPTY = {};
return EMPTY;
}
@Override
public void showFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
setFilePathCallbacks(filePathCallback);
String[] acceptTypes = new String[0];
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
acceptTypes = fileChooserParams.getAcceptTypes();
}
dealOpenFileChooser(acceptTypes.length > 0 ? acceptTypes[0] : "");
}
@Override
public void showFileChooser(ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
this.filePathCallback = filePathCallback;
try {
Intent intent = fileChooserParams.createIntent();
startActivityForResult(intent, IntentUnit.REQUEST_FILE_21);
} catch (Exception e) {
NinjaToast.show(this, R.string.toast_open_file_manager_failed);
}
}
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
uploadMessageAboveL = filePathCallback;
openImageChooserActivity();
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
openFileChooserImplForAndroid5(uploadMsg);
return true;
}
public Builder setFileChooserParams(WebChromeClient.FileChooserParams fileChooserParams) {
mFileChooserParams = fileChooserParams;
return this;
}
static boolean showFileChooserCompat(Activity activity,
WebView webView,
ValueCallback<Uri[]> valueCallbacks,
WebChromeClient.FileChooserParams fileChooserParams,
PermissionInterceptor permissionInterceptor,
ValueCallback valueCallback,
String mimeType,
Handler.Callback jsChannelCallback
) {
try {
Class<?> clz = Class.forName("com.just.agentweb.filechooser.FileChooser");
Object mFileChooser$Builder = clz.getDeclaredMethod("newBuilder",
Activity.class, WebView.class)
.invoke(null, activity, webView);
clz = mFileChooser$Builder.getClass();
Method mMethod = null;
if (valueCallbacks != null) {
mMethod = clz.getDeclaredMethod("setUriValueCallbacks", ValueCallback.class);
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser$Builder, valueCallbacks);
}
if (fileChooserParams != null) {
mMethod = clz.getDeclaredMethod("setFileChooserParams", WebChromeClient.FileChooserParams.class);
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser$Builder, fileChooserParams);
}
if (valueCallback != null) {
mMethod = clz.getDeclaredMethod("setUriValueCallback", ValueCallback.class);
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser$Builder, valueCallback);
}
if (!TextUtils.isEmpty(mimeType)) {
// LogUtils.i(TAG, Arrays.toString(clz.getDeclaredMethods()));
mMethod = clz.getDeclaredMethod("setAcceptType", String.class);
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser$Builder, mimeType);
}
if (jsChannelCallback != null) {
mMethod = clz.getDeclaredMethod("setJsChannelCallback", Handler.Callback.class);
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser$Builder, jsChannelCallback);
}
mMethod = clz.getDeclaredMethod("setPermissionInterceptor", PermissionInterceptor.class);
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser$Builder, permissionInterceptor);
mMethod = clz.getDeclaredMethod("build");
mMethod.setAccessible(true);
Object mFileChooser = mMethod.invoke(mFileChooser$Builder);
mMethod = mFileChooser.getClass().getDeclaredMethod("openFileChooser");
mMethod.setAccessible(true);
mMethod.invoke(mFileChooser);
} catch (Throwable throwable) {
if (LogUtils.isDebug()) {
throwable.printStackTrace();
}
if (throwable instanceof ClassNotFoundException) {
LogUtils.e(TAG, "Please check whether compile'com.just.agentweb:filechooser:x.x.x' dependency was added.");
}
if (valueCallbacks != null) {
LogUtils.i(TAG, "onReceiveValue empty");
return false;
}
if (valueCallback != null) {
valueCallback.onReceiveValue(null);
}
}
return true;
}
public boolean onShowFileChooser(WebView view, ValueCallback<Uri[]> filePath, WebChromeClient.FileChooserParams fileChooserParams) {
// Double check that we don't have any existing callbacks
if (mUploadMessage != null) {
mUploadMessage.onReceiveValue(null);
}
mUploadMessage = filePath;
Log.e("FileCooserParams => ", filePath.toString());
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.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if (takePictureIntent != null) {
intentArray = new Intent[]{takePictureIntent};
} else {
intentArray = new Intent[2];
}
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(Intent.createChooser(chooserIntent, "Select images"), 1);
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
mUIController.showFileChooser(filePathCallback);
return true;
}
public boolean onShowFileChooser(
WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
requestStoragePermission();
if (!hasStoragePermission())
return false;
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// create the file where the photo should go
File photoFile;
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
// 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("*/*");
contentSelectionIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*", "*/*"});
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, "Choose file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
return true;
}
public boolean onShowFileChooser(
WebView mWebView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
requestStoragePermission();
if (!hasStoragePermission())
return false;
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// create the file where the photo should go
File photoFile;
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
// 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("*/*");
contentSelectionIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/*", "video/*", "*/*"});
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, "Choose file");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
return false;
}
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
/** Request permission for external storage access.
* If granted it's awesome and go on,
* otherwise just stop here and leave the method.
*/
requestStoragePermission();
if (!hasStoragePermission())
return false;
if (mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;
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("*/*");
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, getString(R.string.image_chooser));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
return true;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
mUIController.showFileChooser(filePathCallback);
return true;
}
@Override
public void showFileChooser(ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {}
void showFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams);
boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams);