| 1 | // set fields on your Activity |
| 2 | public static final int INPUT_FILE_REQUEST_CODE = 1; |
| 3 | private static final int CAMERA_PERMISSION_REQUEST = 1111; |
| 4 | private PermissionRequest cameraPermission; |
| 5 | private ValueCallback<Uri[]> filePathCallback; |
| 6 | private String cameraPhotoPath; |
| 7 | |
| 8 | @Override |
| 9 | public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 10 | if (requestCode != INPUT_FILE_REQUEST_CODE || filePathCallback == null) { |
| 11 | super.onActivityResult(requestCode, resultCode, data); |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | Uri[] results = null; |
| 16 | |
| 17 | // Check that the response is a good one |
| 18 | if (resultCode == Activity.RESULT_OK) { |
| 19 | if (data == null) { |
| 20 | // If there is not data, then we may have taken a photo |
| 21 | if (cameraPhotoPath != null) { |
| 22 | results = new Uri[] { Uri.parse(cameraPhotoPath) }; |
| 23 | } |
| 24 | } else { |
| 25 | String dataString = data.getDataString(); |
| 26 | if (dataString != null) { |
| 27 | results = new Uri[] { Uri.parse(dataString) }; |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | filePathCallback.onReceiveValue(results); |
| 33 | filePathCallback = null; |
| 34 | } |
| 35 | |
| 36 | // in the Activity#onCreate method |
| 37 | webView.setWebChromeClient(new WebChromeClient() { |
| 38 | @Override |
| 39 | public void onPermissionRequest(final PermissionRequest request) { |
| 40 | if (request.getOrigin().toString().equals("https://withpersona.com/")) { |
| 41 | ActivityCompat.requestPermissions(MainActivity.this, |
| 42 | new String[] { Manifest.permission.CAMERA }, CAMERA_PERMISSION_REQUEST); |
| 43 | cameraPermission = request; |
| 44 | } else { |
| 45 | request.deny(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public boolean onShowFileChooser( |
| 51 | WebView webView, ValueCallback<Uri[]> newFilePathCallback, |
| 52 | FileChooserParams fileChooserParams) { |
| 53 | |
| 54 | if (filePathCallback != null) { |
| 55 | filePathCallback.onReceiveValue(null); |
| 56 | } |
| 57 | filePathCallback = newFilePathCallback; |
| 58 | |
| 59 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 60 | if (takePictureIntent.resolveActivity(getPackageManager()) != null) { |
| 61 | // Create the File where the photo should go |
| 62 | File photoFile = null; |
| 63 | // Create an image file name |
| 64 | String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date()); |
| 65 | String imageFileName = "JPEG_" + timeStamp + "_"; |
| 66 | File storageDir = |
| 67 | Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); |
| 68 | try { |
| 69 | photoFile = File.createTempFile(imageFileName, ".jpg", storageDir); |
| 70 | } catch (IOException ex) { |
| 71 | // Error occurred while creating the File |
| 72 | } |
| 73 | |
| 74 | // Continue only if the File was successfully created |
| 75 | if (photoFile != null) { |
| 76 | cameraPhotoPath = "file:" + photoFile.getAbsolutePath(); |
| 77 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, |
| 78 | Uri.fromFile(photoFile)); |
| 79 | } else { |
| 80 | takePictureIntent = null; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT); |
| 85 | contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE); |
| 86 | contentSelectionIntent.setType("image/*"); |
| 87 | |
| 88 | Intent[] intentArray; |
| 89 | if (takePictureIntent != null) { |
| 90 | intentArray = new Intent[] { takePictureIntent }; |
| 91 | } else { |
| 92 | intentArray = new Intent[0]; |
| 93 | } |
| 94 | |
| 95 | Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER); |
| 96 | chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent); |
| 97 | chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser"); |
| 98 | chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray); |
| 99 | |
| 100 | startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE); |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | // overwriting your AppCompatActivity's #onRequestPermissionsResult |
| 107 | @Override |
| 108 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| 109 | @NonNull int[] grantResults) { |
| 110 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 111 | if (requestCode == CAMERA_PERMISSION_REQUEST) { |
| 112 | if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 113 | cameraPermission.grant(cameraPermission.getResources()); |
| 114 | } else { |
| 115 | cameraPermission.deny(); |
| 116 | } |
| 117 | } |
| 118 | } |