How to Fix Android Webview Can’t Upload Image From Camera Directly
If you have implemented the code to support file upload in android Webview, but still unable to upload images, or if you can upload images from gallery but not from camera, then the issue is most likely error with permissions. If you are looking to make your android webview support file upload, see This page
I got in trouble with camera upload, my app simply wouldn’t be able to get a picture from camera, i mean the camera would not be shown in the file picker and i spent hours thinking my android webview upload code caught some error. I had the permissions set in manifest but this wasn’t enough, i needed ask user permissions and finally i figured it out.
Starting from marshmallow, android requires user permissions to access camera and external storage. Without these permissions, the Webview app can’t initiate a camera upload, so you have to make the user to grant permissions, So here is how to fix camera upload in android Webview
Code to fix Android camera image upload error
In your webview activity, add this code below the OnCreate method.
if(!hasPermissions(this, PERMISSIONS)){ ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL); }
Then add this code in the activity, that is it. Remember that you will have to declare permissions in your android manifest too
int PERMISSION_ALL = 1; String[] PERMISSIONS = { android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.CAMERA }; public static boolean hasPermissions(Context context, String... permissions) { if (context != null && permissions != null) { for (String permission : permissions) { if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) { return false; } } } return true; }}
So this is it, this code is a clever one because it is easier, and if you want more permissions for your app, you can assign those in this code under String[]. Android will ask only required permissions in a sequence when user opens the activity.
Nice solution for Android 10 ,it worked for me thanks