Last active
February 19, 2024 20:00
-
-
Save gokhanaliccii/9942ab3b12d41877cd5e1954c47884c6 to your computer and use it in GitHub Desktop.
WebView File Upload
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
// For Android < 3.0 | |
@SuppressWarnings("unused") | |
public void openFileChooser(ValueCallback<Uri> uploadMsg) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
i.addCategory(Intent.CATEGORY_OPENABLE); | |
i.setType("*/*"); | |
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY); | |
} | |
// For Android 3.0+ | |
@SuppressWarnings("unused") | |
public void openFileChooser(ValueCallback uploadMsg, String acceptType) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
i.addCategory(Intent.CATEGORY_OPENABLE); | |
i.setType(acceptType); | |
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY); | |
} | |
// For Android 4.1+ | |
@SuppressWarnings("unused") | |
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
webView.setWebChromeClient(new WebChromeClient() { | |
// For Android < 3.0 | |
@SuppressWarnings("unused") | |
public void openFileChooser(ValueCallback<Uri> uploadMsg) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
i.addCategory(Intent.CATEGORY_OPENABLE); | |
i.setType("*/*"); | |
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY); | |
} | |
// For Android 3.0+ | |
@SuppressWarnings("unused") | |
public void openFileChooser(ValueCallback uploadMsg, String acceptType) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
i.addCategory(Intent.CATEGORY_OPENABLE); | |
i.setType(acceptType); | |
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY); | |
} | |
// For Android 4.1+ | |
@SuppressWarnings("unused") | |
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { | |
mUploadMessage = uploadMsg; | |
Intent i = new Intent(Intent.ACTION_GET_CONTENT); | |
i.addCategory(Intent.CATEGORY_OPENABLE); | |
i.setType(acceptType); | |
startActivityForResult(Intent.createChooser(i, getString(R.string.select_file)), REQUEST_SELECT_FILE_LEGACY); | |
} | |
// For Android 5.0+ | |
@SuppressLint("NewApi") | |
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) { | |
if (mUploadMessage != null) { | |
mUploadMessage.onReceiveValue(null); | |
mUploadMessage = null; | |
} | |
mUploadMessageArr = filePathCallback; | |
Intent intent = fileChooserParams.createIntent(); | |
try { | |
startActivityForResult(intent, REQUEST_SELECT_FILE); | |
} catch (ActivityNotFoundException e) { | |
mUploadMessageArr = null; | |
return false; | |
} | |
return true; | |
} | |
}); | |
///////////// | |
@SuppressLint("NewApi") | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
if (requestCode == REQUEST_SELECT_FILE_LEGACY) { | |
if (mUploadMessage == null) return; | |
Uri result = data == null || resultCode != Activity.RESULT_OK ? null : data.getData(); | |
mUploadMessage.onReceiveValue(result); | |
mUploadMessage = null; | |
} else if (requestCode == REQUEST_SELECT_FILE) { | |
if (mUploadMessageArr == null) return; | |
mUploadMessageArr.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data)); | |
mUploadMessageArr = null; | |
} | |
} | |
How to add this code on my existing webview I. Android studio???
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi. Where to add this code ? Please help me. I have code for webview and I want a file upload code. Please kindly help me in urgent