You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@cordova.apache.org by "Darron Park (JIRA)" <ji...@apache.org> on 2017/01/03 02:30:58 UTC
[jira] [Commented] (CB-9148) Add support for file picker in
InAppBrowser
[ https://issues.apache.org/jira/browse/CB-9148?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15793939#comment-15793939 ]
Darron Park commented on CB-9148:
---------------------------------
Now I Finally Created Working Code.
This code below is tested only in Android Marshmellow(6.0).
So if anyone has Lollipop or Kitkat device, it would be nice to help me with testing it :)
{quote}
First, I imported some Classes
{quote}
{code:title=InAppBrowser.java|borderStyle=solid}
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
{code}
{quote}
Second, I added some variables to InAppBrowser class.
{quote}
{code:title=InAppBrowser.java|borderStyle=solid}
private ValueCallback<Uri> mUploadCallback;
private ValueCallback<Uri[]> mUploadCallbackLollipop;
private final static int FILECHOOSER_REQUESTCODE = 1;
private final static int FILECHOOSER_REQUESTCODE_LOLLIPOP = 2;
{code}
{quote}
Then, I changed the code from...
{quote}
{code:title=InAppBrowser.java(before)|borderStyle=solid}
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView));
{code}
{quote}
to...
{quote}
{code:title=InAppBrowser.java(after)|borderStyle=solid}
// File Chooser Implemented ChromeClient
inAppWebView.setWebChromeClient(new InAppChromeClient(thatWebView) {
// For Android 5.0+
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
{
Log.d(LOG_TAG, "File Chooser 5.0+");
// If callback exists, finish it.
if(mUploadCallbackLollipop != null) {
mUploadCallbackLollipop.onReceiveValue(null);
}
mUploadCallbackLollipop = filePathCallback;
// Create File Chooser Intent
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);
content.setType("*/*");
// [important] Register ActvityResultCallback on Cordova
cordova.setActivityResultCallback(InAppBrowser.this);
cordova.getActivity().startActivityForResult(content, FILECHOOSER_REQUESTCODE_LOLLIPOP);
return true;
}
// For Android 4.1+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
{
Log.d(LOG_TAG, "File Chooser 4.1+");
// Call file chooser for Android 3.0+
openFileChooser(uploadMsg, acceptType);
}
// For Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType)
{
Log.d(LOG_TAG, "File Chooser 3.0+");
mUploadCallback = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
cordova.setActivityResultCallback(InAppBrowser.this);
cordova.getActivity().startActivityForResult( Intent.createChooser(i, "File Chooser"), FILECHOOSER_REQUESTCODE);
}
// For Android < 3.0
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
Log.d(LOG_TAG, "File Chooser 3.0 <");
// Call file chooser for Android 3.0+
openFileChooser(uploadMsg, "");
}
});
{code}
{quote}
I Also added method below(onActivityResult) to InAppBrowser class
{quote}
{code:title=InAppBrowser.java(after)|borderStyle=solid}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// For Android >= 5.0
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Log.i("mytag", "onActivityResult (For Android >= 5.0)");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE_LOLLIPOP || mUploadCallbackLollipop == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
mUploadCallbackLollipop.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallbackLollipop = null;
}
// For Android < 5.0
else {
Log.i("mytag", "onActivityResult (For Android < 5.0)");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
return;
}
if (null == mUploadCallback) return;
Uri result = intent == null || resultCode != cordova.getActivity().RESULT_OK ? null : intent.getData();
mUploadCallback.onReceiveValue(result);
mUploadCallback = null;
}
}
{code}
Now you are done.
In my case, it worked well with no error.
Give it a try and any comment will be appreciated
Happy New Year:D
> Add support for file picker in InAppBrowser
> -------------------------------------------
>
> Key: CB-9148
> URL: https://issues.apache.org/jira/browse/CB-9148
> Project: Apache Cordova
> Issue Type: Improvement
> Components: Plugin InAppBrowser
> Affects Versions: 3.5.0, 3.6.0
> Environment: problem in Android, the camera roll never opens (iOS working OK)
> Reporter: Tomas Rawski
> Labels: Android, camera, file, input, roll, triaged
>
> reproduce :
> open the inAppBrowser: http://www.plupload.com/examples
> click in Add files.
> This works ok when I call it from the _system or _self. But not for _blank.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org
For additional commands, e-mail: issues-help@cordova.apache.org