Created
October 10, 2015 05:48
-
-
Save mnafian/cc6bdae896c0343d79f9 to your computer and use it in GitHub Desktop.
Webview support for upload image or file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void initWebView(final String linkURL) { | |
pDialog.show(); | |
mWebView = (WebView) findViewById(R.id.content_webview); | |
mWebView.getSettings().setJavaScriptEnabled(true); | |
mWebView.loadUrl(linkURL); | |
mWebView.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; | |
// Set up the take picture intent | |
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
if (takePictureIntent.resolveActivity(SettingFaqAndPrivacyAndConfirm.this.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 | |
} | |
// 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, INPUT_FILE_REQUEST_CODE); | |
return true; | |
} | |
}); | |
mWebView.setWebViewClient(new WebViewClient() { | |
@Override | |
public void onReceivedError(WebView view, int errorCode, | |
String description, String failingUrl) { | |
super.onReceivedError(view, errorCode, description, failingUrl); | |
} | |
@Override | |
public void onPageFinished(WebView view, String url) { | |
super.onPageFinished(view, url); | |
pDialog.dismiss(); | |
} | |
}); | |
} | |
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 = Environment.getExternalStoragePublicDirectory( | |
Environment.DIRECTORY_PICTURES); | |
File imageFile = File.createTempFile( | |
imageFileName, /* prefix */ | |
".jpg", /* suffix */ | |
storageDir /* directory */ | |
); | |
return imageFile; | |
} | |
/** | |
* Convenience method to set some generic defaults for a | |
* given WebView | |
* | |
* @param webView | |
*/ | |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) | |
private void setUpWebViewDefaults(WebView webView) { | |
WebSettings settings = webView.getSettings(); | |
// Enable Javascript | |
settings.setJavaScriptEnabled(true); | |
// Use WideViewport and Zoom out if there is no viewport defined | |
settings.setUseWideViewPort(true); | |
settings.setLoadWithOverviewMode(true); | |
// Enable pinch to zoom without the zoom buttons | |
settings.setBuiltInZoomControls(true); | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.HONEYCOMB) { | |
// Hide the zoom controls for HONEYCOMB+ | |
settings.setDisplayZoomControls(false); | |
} | |
// Enable remote debugging via chrome://inspect | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
WebView.setWebContentsDebuggingEnabled(true); | |
} | |
// We set the WebViewClient to ensure links are consumed by the WebView rather | |
// than passed to a browser if it can | |
mWebView.setWebViewClient(new WebViewClient()); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
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; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment