So i made a cordova app, i added android platform and made a simple html with an imput field
Add these permissions into AndroidManifest.xml file
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
Add provider in AndroidManifest.xml file on this area
...
....
Create new xml file in res/xml/file_paths.xml
content:
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 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 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 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;
}