Note that there are some explanatory texts on larger screens.

plurals
  1. POUpload an Image from camera or gallery in WebView
    text
    copied!<p>WebView in this app opens a page with upload button.</p> <p><img src="https://i.stack.imgur.com/K4N5x.png" alt="Page in webview with upload button"></p> <p>Below is the code block that allows to open a dialog box to upload image from gallery or camera.</p> <p><strong>Within my Activity I have:</strong></p> <pre><code> private WebView wv; //make HTML upload button work in Webview private ValueCallback&lt;Uri&gt; mUploadMessage; private final static int FILECHOOSER_RESULTCODE=1; @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if(requestCode==FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } } </code></pre> <p><strong>Within onCreate I have the following:</strong></p> <pre><code> wv.setWebChromeClient(new WebChromeClient() { private Uri imageUri; public void openFileChooser(ValueCallback&lt;Uri&gt; uploadMsg, String acceptType ) { File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp"); // Create the storage directory if it does not exist if (! imageStorageDir.exists()){ imageStorageDir.mkdirs(); } File file = new File(imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg"); imageUri = Uri.fromFile(file); final List&lt;Intent&gt; cameraIntents = new ArrayList&lt;Intent&gt;(); final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); final PackageManager packageManager = getPackageManager(); final List&lt;ResolveInfo&gt; listCam = packageManager.queryIntentActivities(captureIntent, 0); for(ResolveInfo res : listCam) { final String packageName = res.activityInfo.packageName; final Intent i = new Intent(captureIntent); i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); i.setPackage(packageName); i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); cameraIntents.add(i); } mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); Intent chooserIntent = Intent.createChooser(i,"Image Chooser"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{})); MainActivity.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE); } </code></pre> <p>I am able to see option for camera, image gallery and file explorer on clicking Upload button. <img src="https://i.stack.imgur.com/f4OZy.png" alt="Camera, gallery and file explorer upload option"></p> <p>File explorer and Gallery is working as expected. The problem is that, when I take a picture using camera, it is not uploaded in the "choose file" option which shows status "No file chosen".</p> <p><strong>ON SELECTING CAMERA:</strong></p> <p><img src="https://i.stack.imgur.com/pzdR1.png" alt="camera"></p> <p><strong>ON TAKING SNAPSHOT USING CAMERA:</strong> back and check options appear.</p> <p><img src="https://i.stack.imgur.com/Ksnwq.png" alt="snapshot using camera"></p> <p><strong>ON CHOOSING CHECK MARK:</strong></p> <p><strong>FILE IS NOT UPLOADED :( IN "CHOOSE FILE" OPTION</strong></p> <p><img src="https://i.stack.imgur.com/3GQ9R.png" alt="enter image description here"></p> <p><strong>WHAT IS EXPECTED:</strong></p> <p><img src="https://i.stack.imgur.com/F3gRn.png" alt="image uploaded"></p> <p>I checked that I have the proper writing permission and hence a directory named "MyApp" is generated and the picture is stored within it (if taken by invoking camera after clicking upload button on webpage). </p> <p>How to programatically tell the application to choose picture taken from camera (that was stored in MyApp directory) after hitting check mark?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload