Fix Android Webview Camera Upload Error

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.

Related Posts

Fix Plugin [id: ‘com.android.application’, version: ‘7.1.3’, apply: false] was not found in any of the following sources

Here is how to fix Plugin [id: ‘com.android.application’, version: ‘7.1.3’, apply: false] was not found in any of the following sources This error is caused by mismatching…

Get Age from Date of Birth in Java Android

Here is how to get a user’s age from date of birth If you need get a user’s age from a given date of birth string, this…

How to Check if String Array Contains a Specific String in Android

Here is how to get a specific value or String from Array in Android To check if a string array contains a specific string, you can use…

Make Android WebView Support Razorpay Payment

How to Support Razorpay Payment and UPI in Android WebView? Android webview is inexpensive and easy way to make apps for websites. But to support some specific…

Make Android Webview Support PayTM Google Pay PhonePe UPI

GooglePay, Phonpe and PayTM support in Android WebView If you have built an android app for your new ecommerce website or any other website that needs accept…

This Post Has One Comment

Leave a Reply

Your email address will not be published.