@@ -84,6 +84,16 @@ Licensed to the Apache Software Foundation (ASF) under one
84
84
import java .util .HashMap ;
85
85
import java .util .StringTokenizer ;
86
86
87
+ import android .Manifest ;
88
+ import android .os .Environment ;
89
+ import android .provider .MediaStore ;
90
+ import android .util .Log ;
91
+ import androidx .core .content .FileProvider ;
92
+ import java .io .File ;
93
+ import java .io .IOException ;
94
+ import java .text .SimpleDateFormat ;
95
+ import java .util .Date ;
96
+
87
97
@ SuppressLint ("SetJavaScriptEnabled" )
88
98
public class InAppBrowser extends CordovaPlugin {
89
99
@@ -151,6 +161,8 @@ public class InAppBrowser extends CordovaPlugin {
151
161
private boolean fullscreen = true ;
152
162
private String [] allowedSchemes ;
153
163
private InAppBrowserClient currentClient ;
164
+ private String photoFilePath ;
165
+ private Uri photoFileUri ;
154
166
155
167
/**
156
168
* Executes the request and returns PluginResult.
@@ -925,19 +937,61 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
925
937
public boolean onShowFileChooser (WebView webView , ValueCallback <Uri []> filePathCallback , WebChromeClient .FileChooserParams fileChooserParams )
926
938
{
927
939
LOG .d (LOG_TAG , "File Chooser 5.0+" );
940
+ if (Build .VERSION .SDK_INT >= 23 && (cordova .getActivity ().checkSelfPermission (
941
+ Manifest .permission .WRITE_EXTERNAL_STORAGE ) != PackageManager .PERMISSION_GRANTED
942
+ || cordova .getActivity ().checkSelfPermission (
943
+ Manifest .permission .CAMERA ) != PackageManager .PERMISSION_GRANTED )) {
944
+ cordova .getActivity ().requestPermissions (new String [] {
945
+ Manifest .permission .WRITE_EXTERNAL_STORAGE , Manifest .permission .CAMERA }, 1 );
946
+ }
947
+
928
948
// If callback exists, finish it.
929
- if (mUploadCallback != null ) {
949
+ if (mUploadCallback != null ) {
930
950
mUploadCallback .onReceiveValue (null );
931
951
}
932
952
mUploadCallback = filePathCallback ;
933
953
954
+ Intent takePictureIntent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
955
+
956
+ if (takePictureIntent .resolveActivity (cordova .getActivity ().getPackageManager ()) != null ) {
957
+
958
+ File photoFile = null ;
959
+ try {
960
+ photoFile = createImageFile ();
961
+ takePictureIntent .putExtra ("PhotoPath" , photoFilePath );
962
+ } catch (IOException ex ) {
963
+ Log .e (LOG_TAG , "Image file creation failed" , ex );
964
+ }
965
+ if (photoFile != null ) {
966
+ photoFilePath = "file:/" + photoFile .getAbsolutePath ();
967
+ photoFileUri = FileProvider .getUriForFile (
968
+ cordova .getActivity ().getApplicationContext (),
969
+ cordova .getActivity ().getPackageName () + ".fileprovider" ,
970
+ photoFile
971
+ );
972
+ takePictureIntent .putExtra (MediaStore .EXTRA_OUTPUT , photoFileUri );
973
+ } else {
974
+ takePictureIntent = null ;
975
+ }
976
+ }
934
977
// Create File Chooser Intent
935
- Intent content = new Intent (Intent .ACTION_GET_CONTENT );
936
- content .addCategory (Intent .CATEGORY_OPENABLE );
937
- content .setType ("*/*" );
978
+ Intent contentSelectionIntent = new Intent (Intent .ACTION_GET_CONTENT );
979
+ contentSelectionIntent .addCategory (Intent .CATEGORY_OPENABLE );
980
+ contentSelectionIntent .setType ("*/*" );
981
+ Intent [] intentArray ;
982
+ if (takePictureIntent != null ) {
983
+ intentArray = new Intent [] { takePictureIntent };
984
+ } else {
985
+ intentArray = new Intent [0 ];
986
+ }
987
+
988
+ Intent chooserIntent = new Intent (Intent .ACTION_CHOOSER );
989
+ chooserIntent .putExtra (Intent .EXTRA_INTENT , contentSelectionIntent );
990
+ chooserIntent .putExtra (Intent .EXTRA_INITIAL_INTENTS , intentArray );
938
991
939
992
// Run cordova startActivityForResult
940
- cordova .startActivityForResult (InAppBrowser .this , Intent .createChooser (content , "Select File" ), FILECHOOSER_REQUESTCODE );
993
+ cordova .startActivityForResult (InAppBrowser .this , chooserIntent , FILECHOOSER_REQUESTCODE );
994
+
941
995
return true ;
942
996
}
943
997
});
@@ -1075,6 +1129,14 @@ public void postMessage(String data) {
1075
1129
return "" ;
1076
1130
}
1077
1131
1132
+ private File createImageFile () throws IOException {
1133
+ @ SuppressLint ("SimpleDateFormat" ) String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss" ).format (new Date ());
1134
+ String imageFileName = "img_" +timeStamp +"_" ;
1135
+ File storageDir = this .cordova .getActivity ().getExternalFilesDir (Environment .DIRECTORY_PICTURES );
1136
+ File file = new File (storageDir , imageFileName + ".jpg" );
1137
+ return file ;
1138
+ }
1139
+
1078
1140
/**
1079
1141
* Create a new plugin success result and send it back to JavaScript
1080
1142
*
@@ -1115,7 +1177,16 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
1115
1177
super .onActivityResult (requestCode , resultCode , intent );
1116
1178
return ;
1117
1179
}
1118
- mUploadCallback .onReceiveValue (WebChromeClient .FileChooserParams .parseResult (resultCode , intent ));
1180
+ Intent customIntent = intent ;
1181
+ // Intent can be null (happens on older versions of Android)
1182
+ if (intent == null ) {
1183
+ customIntent = new Intent ();
1184
+ }
1185
+ if (customIntent .getData () == null ) {
1186
+ // Intent data are empty when using camera
1187
+ customIntent .setData (photoFileUri );
1188
+ }
1189
+ mUploadCallback .onReceiveValue (WebChromeClient .FileChooserParams .parseResult (resultCode , customIntent ));
1119
1190
mUploadCallback = null ;
1120
1191
}
1121
1192
0 commit comments