Add these permissions into AndroidManifest.xml file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Add these permission requests into the MainActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
Add android:requestLegacyExternalStorage="true" into AndroidManifest.xml file on this tag
<application
android:requestLegacyExternalStorage="true"
Add provider in AndroidManifest.xml file on this area
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
....
</application>
Create new xml file in res/xml/file_paths.xml
content:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
Add these lines end of CordovaLib/build.gradle file
dependencies {
implementation 'com.android.support:support-v4:28.0.0'
}
Change the SystemWebChromeClient.java file like this
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
Intent intent = createChooserIntentWithImageSelection();
try {
parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
Uri[] result = WebChromeClient.FileChooserParams.parseResult(resultCode, intent);
if(result==null){
if(mCameraPhotoPath!=null && Uri.parse(mCameraPhotoPath)!=null) {
File returnFile = new File(Uri.parse(mCameraPhotoPath).getPath());
if (returnFile.length() > 0) {
result = new Uri[1];
result[0] = Uri.parse(mCameraPhotoPath);
}
}
}
LOG.d(LOG_TAG, "Receive file chooser URL: " + result);
filePathsCallback.onReceiveValue(result);
}
}, intent, INPUT_FILE_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
LOG.w("No activity found to handle file chooser intent.", e);
filePathsCallback.onReceiveValue(null);
}
return true;
}
private static final String PATH_PREFIX = "file:";
public Intent createChooserIntentWithImageSelection() {
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
ArrayList<Intent> extraIntents = new ArrayList<>();
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
File photoFile = createImageFile();
if (photoFile != null) {
mCameraPhotoPath = PATH_PREFIX + photoFile.getAbsolutePath();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
FileProvider.getUriForFile(appContext,
appContext.getPackageName() + ".provider",
photoFile));
}
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
if (takePictureIntent != null) {
extraIntents.add(takePictureIntent);
}
if (!extraIntents.isEmpty()) {
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
extraIntents.toArray(new Intent[]{}));
}
return chooserIntent;
}
//creating temp picture file
private File createImageFile() {
String state = Environment.getExternalStorageState();
if (!state.equals(Environment.MEDIA_MOUNTED)) {
Log.e(TAG, "External storage is not mounted.");
return null;
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalSdCardPath();
storageDir.mkdirs();
try {
File file = File.createTempFile(imageFileName, ".jpg", storageDir);
Log.d(TAG, "Created image file: " + file.getAbsolutePath());
return file;
} catch (IOException e) {
Log.e(TAG, "Unable to create Image File, " +
"please make sure permission 'WRITE_EXTERNAL_STORAGE' was added.");
return null;
}
}
//for external sd card check
public static File getExternalSdCardPath() {
String path = null;
File sdCardFile = null;
List<String> sdCardPossiblePath = Arrays.asList("external_sd", "ext_sd", "external", "extSdCard");
for (String sdPath : sdCardPossiblePath) {
File file = new File("/mnt/", sdPath);
if (file.isDirectory() && file.canWrite()) {
path = file.getAbsolutePath();
String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
File testWritable = new File(path, "test_" + timeStamp);
if (testWritable.mkdirs()) {
testWritable.delete();
} else {
path = null;
}
}
}
if (path != null) {
sdCardFile = new File(path);
} else {
sdCardFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
}
return sdCardFile;
}