Skip to content

Implementation of input type file chooser to allow to capture, record and upload file #1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
</feature>
</config-file>

<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.CAMERA" required="true" />
</config-file>

<source-file src="src/android/InAppBrowser.java" target-dir="src/org/apache/cordova/inappbrowser" />
<source-file src="src/android/InAppBrowserDialog.java" target-dir="src/org/apache/cordova/inappbrowser" />
<source-file src="src/android/InAppChromeClient.java" target-dir="src/org/apache/cordova/inappbrowser" />
Expand Down
95 changes: 84 additions & 11 deletions src/android/InAppBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.InputType;
import android.util.TypedValue;
import android.view.Gravity;
Expand Down Expand Up @@ -65,23 +66,29 @@ Licensed to the Apache Software Foundation (ASF) under one

import org.apache.cordova.CallbackContext;
import org.apache.cordova.Config;
import org.apache.cordova.BuildHelper;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaHttpAuthHandler;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.LOG;
import org.apache.cordova.PluginManager;
import org.apache.cordova.PluginResult;
import org.apache.cordova.camera.FileProvider;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashMap;
import java.util.Date;
import java.util.StringTokenizer;

@SuppressLint("SetJavaScriptEnabled")
Expand Down Expand Up @@ -151,6 +158,7 @@ public class InAppBrowser extends CordovaPlugin {
private boolean fullscreen = true;
private String[] allowedSchemes;
private InAppBrowserClient currentClient;
private File tempImageFile;

/**
* Executes the request and returns PluginResult.
Expand Down Expand Up @@ -930,14 +938,35 @@ public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePath
mUploadCallback.onReceiveValue(null);
}
mUploadCallback = filePathCallback;

// Create File Chooser Intent
Intent content = new Intent(Intent.ACTION_GET_CONTENT);
content.addCategory(Intent.CATEGORY_OPENABLE);
content.setType("*/*");

// Run cordova startActivityForResult
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);

// File chooser implementation to show camera, camcorder and file browser as option to choose from
String applicationId = (String) BuildHelper.getBuildConfigValue(cordova.getActivity(), "APPLICATION_ID");
applicationId = preferences.getString("applicationId", applicationId);

// camera intent
Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
tempImageFile = createTempFile(".jpg");
Uri intentCameraOutputUri = FileProvider.getUriForFile(cordova.getActivity(), applicationId + ".fileprovider", tempImageFile);
pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, intentCameraOutputUri);

// video intent
Intent videoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

// Files intent

Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");

Intent[] intentArray = new Intent[]{pictureIntent, videoIntent, contentSelectionIntent};

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, pictureIntent);
chooserIntent.putExtra(Intent.EXTRA_INTENT, videoIntent);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Choose an action");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
cordova.startActivityForResult(InAppBrowser.this, chooserIntent, FILECHOOSER_REQUESTCODE);
return true;
}
});
Expand Down Expand Up @@ -1075,6 +1104,30 @@ public void postMessage(String data) {
return "";
}

/**
* Create temp file to store the choose file
*
* @param String to create a file with extension of
*/
private File createTempFile(String extension) {
File fileDir = cordova.getActivity().getExternalCacheDir();
File file;
try {
if (!fileDir.isDirectory() || !fileDir.exists())
fileDir.mkdir();
} catch (Exception e) {
return null;
}
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "camera_" + timeStamp;
file = new File(fileDir, imageFileName + extension);
} catch (Exception e) {
return null;
}
return file;
}

/**
* Create a new plugin success result and send it back to JavaScript
*
Expand Down Expand Up @@ -1111,12 +1164,32 @@ private void sendUpdate(JSONObject obj, boolean keepCallback, PluginResult.Statu
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
LOG.d(LOG_TAG, "onActivityResult");
// If RequestCode or Callback is Invalid
if(requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
if (resultCode == 0){ // cancelled
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallback = null;
return;
}

// handle the selected photo, video or file
if (requestCode == FILECHOOSER_REQUESTCODE && resultCode == -1 && intent == null) {
mUploadCallback.onReceiveValue(new Uri[]{Uri.fromFile(tempImageFile)});
mUploadCallback = null;
return;
}
else if (requestCode == FILECHOOSER_REQUESTCODE && intent.getData() != null) { // video/file
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallback = null;
return;
}
else if (requestCode != FILECHOOSER_REQUESTCODE || mUploadCallback == null) {
super.onActivityResult(requestCode, resultCode, intent);
mUploadCallback = null;
return;
}
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallback = null;
else {
mUploadCallback.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));
mUploadCallback = null;
}
}

/**
Expand Down