@@ -83,6 +83,16 @@ Licensed to the Apache Software Foundation (ASF) under one
83
83
import java .util .HashMap ;
84
84
import java .util .StringTokenizer ;
85
85
86
+ import android .Manifest ;
87
+ import android .os .Environment ;
88
+ import android .provider .MediaStore ;
89
+ import android .util .Log ;
90
+ import androidx .core .content .FileProvider ;
91
+ import java .io .File ;
92
+ import java .io .IOException ;
93
+ import java .text .SimpleDateFormat ;
94
+ import java .util .Date ;
95
+
86
96
@ SuppressLint ("SetJavaScriptEnabled" )
87
97
public class InAppBrowser extends CordovaPlugin {
88
98
@@ -147,6 +157,8 @@ public class InAppBrowser extends CordovaPlugin {
147
157
private boolean fullscreen = true ;
148
158
private String [] allowedSchemes ;
149
159
private InAppBrowserClient currentClient ;
160
+ private String photoFilePath ;
161
+ private Uri photoFileUri ;
150
162
151
163
/**
152
164
* Executes the request and returns PluginResult.
@@ -921,19 +933,61 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
921
933
public boolean onShowFileChooser (WebView webView , ValueCallback <Uri []> filePathCallback , WebChromeClient .FileChooserParams fileChooserParams )
922
934
{
923
935
LOG .d (LOG_TAG , "File Chooser 5.0+" );
936
+ if (Build .VERSION .SDK_INT >= 23 && (cordova .getActivity ().checkSelfPermission (
937
+ Manifest .permission .WRITE_EXTERNAL_STORAGE ) != PackageManager .PERMISSION_GRANTED
938
+ || cordova .getActivity ().checkSelfPermission (
939
+ Manifest .permission .CAMERA ) != PackageManager .PERMISSION_GRANTED )) {
940
+ cordova .getActivity ().requestPermissions (new String [] {
941
+ Manifest .permission .WRITE_EXTERNAL_STORAGE , Manifest .permission .CAMERA }, 1 );
942
+ }
943
+
924
944
// If callback exists, finish it.
925
- if (mUploadCallback != null ) {
945
+ if (mUploadCallback != null ) {
926
946
mUploadCallback .onReceiveValue (null );
927
947
}
928
948
mUploadCallback = filePathCallback ;
929
949
950
+ Intent takePictureIntent = new Intent (MediaStore .ACTION_IMAGE_CAPTURE );
951
+
952
+ if (takePictureIntent .resolveActivity (cordova .getActivity ().getPackageManager ()) != null ) {
953
+
954
+ File photoFile = null ;
955
+ try {
956
+ photoFile = createImageFile ();
957
+ takePictureIntent .putExtra ("PhotoPath" , photoFilePath );
958
+ } catch (IOException ex ) {
959
+ Log .e (LOG_TAG , "Image file creation failed" , ex );
960
+ }
961
+ if (photoFile != null ) {
962
+ photoFilePath = "file:/" + photoFile .getAbsolutePath ();
963
+ photoFileUri = FileProvider .getUriForFile (
964
+ cordova .getActivity ().getApplicationContext (),
965
+ cordova .getActivity ().getPackageName () + ".fileprovider" ,
966
+ photoFile
967
+ );
968
+ takePictureIntent .putExtra (MediaStore .EXTRA_OUTPUT , photoFileUri );
969
+ } else {
970
+ takePictureIntent = null ;
971
+ }
972
+ }
930
973
// Create File Chooser Intent
931
- Intent content = new Intent (Intent .ACTION_GET_CONTENT );
932
- content .addCategory (Intent .CATEGORY_OPENABLE );
933
- content .setType ("*/*" );
974
+ Intent contentSelectionIntent = new Intent (Intent .ACTION_GET_CONTENT );
975
+ contentSelectionIntent .addCategory (Intent .CATEGORY_OPENABLE );
976
+ contentSelectionIntent .setType ("*/*" );
977
+ Intent [] intentArray ;
978
+ if (takePictureIntent != null ) {
979
+ intentArray = new Intent [] { takePictureIntent };
980
+ } else {
981
+ intentArray = new Intent [0 ];
982
+ }
983
+
984
+ Intent chooserIntent = new Intent (Intent .ACTION_CHOOSER );
985
+ chooserIntent .putExtra (Intent .EXTRA_INTENT , contentSelectionIntent );
986
+ chooserIntent .putExtra (Intent .EXTRA_INITIAL_INTENTS , intentArray );
934
987
935
988
// Run cordova startActivityForResult
936
- cordova .startActivityForResult (InAppBrowser .this , Intent .createChooser (content , "Select File" ), FILECHOOSER_REQUESTCODE );
989
+ cordova .startActivityForResult (InAppBrowser .this , chooserIntent , FILECHOOSER_REQUESTCODE );
990
+
937
991
return true ;
938
992
}
939
993
});
@@ -1047,6 +1101,14 @@ public void postMessage(String data) {
1047
1101
return "" ;
1048
1102
}
1049
1103
1104
+ private File createImageFile () throws IOException {
1105
+ @ SuppressLint ("SimpleDateFormat" ) String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss" ).format (new Date ());
1106
+ String imageFileName = "img_" +timeStamp +"_" ;
1107
+ File storageDir = this .cordova .getActivity ().getExternalFilesDir (Environment .DIRECTORY_PICTURES );
1108
+ File file = new File (storageDir , imageFileName + ".jpg" );
1109
+ return file ;
1110
+ }
1111
+
1050
1112
/**
1051
1113
* Create a new plugin success result and send it back to JavaScript
1052
1114
*
@@ -1087,6 +1149,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
1087
1149
super .onActivityResult (requestCode , resultCode , intent );
1088
1150
return ;
1089
1151
}
1152
+ // Intent data are empty when using camera
1153
+ if (intent != null && intent .getData () == null ) {
1154
+ intent .setData (photoFileUri );
1155
+ }
1090
1156
mUploadCallback .onReceiveValue (WebChromeClient .FileChooserParams .parseResult (resultCode , intent ));
1091
1157
mUploadCallback = null ;
1092
1158
}
0 commit comments