Skip to content

Commit df841d3

Browse files
Lukáš Havlíčekmoklett
authored andcommitted
apacheGH-328 android: handle photo from camera
1 parent 2b72889 commit df841d3

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

src/android/InAppBrowser.java

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ Licensed to the Apache Software Foundation (ASF) under one
8484
import java.util.HashMap;
8585
import java.util.StringTokenizer;
8686

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+
8797
@SuppressLint("SetJavaScriptEnabled")
8898
public class InAppBrowser extends CordovaPlugin {
8999

@@ -151,6 +161,8 @@ public class InAppBrowser extends CordovaPlugin {
151161
private boolean fullscreen = true;
152162
private String[] allowedSchemes;
153163
private InAppBrowserClient currentClient;
164+
private String photoFilePath;
165+
private Uri photoFileUri;
154166

155167
/**
156168
* Executes the request and returns PluginResult.
@@ -925,19 +937,61 @@ public boolean onKey(View v, int keyCode, KeyEvent event) {
925937
public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)
926938
{
927939
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+
928948
// If callback exists, finish it.
929-
if(mUploadCallback != null) {
949+
if (mUploadCallback != null) {
930950
mUploadCallback.onReceiveValue(null);
931951
}
932952
mUploadCallback = filePathCallback;
933953

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+
}
934977
// 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);
938991

939992
// Run cordova startActivityForResult
940-
cordova.startActivityForResult(InAppBrowser.this, Intent.createChooser(content, "Select File"), FILECHOOSER_REQUESTCODE);
993+
cordova.startActivityForResult(InAppBrowser.this, chooserIntent, FILECHOOSER_REQUESTCODE);
994+
941995
return true;
942996
}
943997
});
@@ -1075,6 +1129,14 @@ public void postMessage(String data) {
10751129
return "";
10761130
}
10771131

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+
10781140
/**
10791141
* Create a new plugin success result and send it back to JavaScript
10801142
*
@@ -1115,7 +1177,16 @@ public void onActivityResult(int requestCode, int resultCode, Intent intent) {
11151177
super.onActivityResult(requestCode, resultCode, intent);
11161178
return;
11171179
}
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));
11191190
mUploadCallback = null;
11201191
}
11211192

0 commit comments

Comments
 (0)