WebView webView;
private
ValueCallback<Uri> mUploadMessage;
private
ValueCallback<Uri[]> mFilePathCallback;
private
String mCameraPhotoPath;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
check_permission();
webView = (WebView) findViewById(R.id.main_webview);
webView.getSettings().setBuiltInZoomControls(
true
);
webView.getSettings().setJavaScriptEnabled(
true
);
webView.getSettings().setDomStorageEnabled(
true
);
webView.setWebChromeClient(
new
WebChromeClient() {
@Override
public
void
onCloseWindow(WebView w) {
super
.onCloseWindow(w);
finish();
}
@Override
public
boolean
onCreateWindow(WebView view,
boolean
dialog,
boolean
userGesture, Message resultMsg) {
final
WebSettings settings = view.getSettings();
settings.setDomStorageEnabled(
true
);
settings.setJavaScriptEnabled(
true
);
settings.setAllowFileAccess(
true
);
settings.setAllowContentAccess(
true
);
view.setWebChromeClient(
this
);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(view);
resultMsg.sendToTarget();
return
false
;
}
public
void
openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent intent =
new
Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType(TYPE_IMAGE);
startActivityForResult(intent, INPUT_FILE_REQUEST_CODE);
}
public
void
openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
openFileChooser(uploadMsg, acceptType,
""
);
}
public
void
openFileChooser(ValueCallback<Uri> uploadFile, String acceptType, String capture) {
Log.d(getClass().getName(),
"openFileChooser : "
+ acceptType +
"/"
+ capture);
mUploadMessage = uploadFile;
imageChooser();
}
public
boolean
onShowFileChooser(WebView webView,
ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
System.out.println(
"WebViewActivity A>5, OS Version : "
+ Build.VERSION.SDK_INT +
"\t onSFC(WV,VCUB,FCP), n=3"
);
if
(mFilePathCallback !=
null
) {
mFilePathCallback.onReceiveValue(
null
);
}
mFilePathCallback = filePathCallback;
imageChooser();
return
true
;
}
private
void
imageChooser() {
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) {
Log.e(getClass().getName(),
"Unable to create Image File"
, 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(TYPE_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);
}
});
webView.setWebViewClient(
new
WebViewClient());
webView.loadUrl(URL_LOAD);
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
if
(requestCode == INPUT_FILE_REQUEST_CODE && resultCode == RESULT_OK) {
if
(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if
(mFilePathCallback ==
null
) {
super
.onActivityResult(requestCode, resultCode, data);
return
;
}
Uri[] results =
new
Uri[]{getResultUri(data)};
mFilePathCallback.onReceiveValue(results);
mFilePathCallback =
null
;
}
else
{
if
(mUploadMessage ==
null
) {
super
.onActivityResult(requestCode, resultCode, data);
return
;
}
Uri result = getResultUri(data);
Log.d(getClass().getName(),
"openFileChooser : "
+ result);
mUploadMessage.onReceiveValue(result);
mUploadMessage =
null
;
}
}
else
{
if
(mFilePathCallback !=
null
) mFilePathCallback.onReceiveValue(
null
);
if
(mUploadMessage !=
null
) mUploadMessage.onReceiveValue(
null
);
mFilePathCallback =
null
;
mUploadMessage =
null
;
super
.onActivityResult(requestCode, resultCode, data);
}
}
}