1

Hi my project requires me to work with webview and camera. I implemented codes in HTML which upon pressing a "Choose File" button, the webview will let me choose whether to capture photo using camera or upload files from my file manager. But I don't want the file option, I just want it to launch camera right after I press the button. This is the code where it asks for permission to use my camera and external storage

public void get_file(){
    String[] perms = {/*Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, */Manifest.permission.CAMERA};

    //Checking for CAMERA Permissions first
    //if (ASWP_CAMUPLOAD && check_permission(3)) {
        //ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, file_perm);

    //Checking for storage permission to write images for upload
    if (ASWP_FUPLOAD && ASWP_CAMUPLOAD && !check_permission(2) && !check_permission(3)) {
        ActivityCompat.requestPermissions(MainActivity.this, perms, file_perm);

    //Checking for WRITE_EXTERNAL_STORAGE permission
    } else if (ASWP_FUPLOAD && !check_permission(2)) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, file_perm);

    //Checking for CAMERA permissions
    } else if (ASWP_CAMUPLOAD && !check_permission(3)) {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, file_perm);
    }
}
public boolean check_permission(int permission){
    switch(permission){
        case 1:
            return ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;

        case 2:
            return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;

        case 3:
            return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;

    }
    return false;
}

And this is the code that I changed

static boolean ASWP_FUPLOAD     = false;     //upload file from webview
static boolean ASWP_ONLYCAM     = true ;     //only use camera as input

I changed the upload file from webview option from true to false. But weird thing is, after I changed it to false, pressing the button never worked anymore. What did I do wrong? If I want to use camera only should I remove some of the lines in public void get_file() ? Hoping for some advises, thank you! Edit: I found one code which is ASWP_ONLYCAM that only enables camera as the only input! But I have no idea how to activate it! p.s. please let me know if you need anymore info

VentusZXC
  • 31
  • 6

1 Answers1

0

As I can see, you are using Android SmartWebView.

I suppose that you support yourself with code from here (ProgramCreek), as your methods are so far the same. if so, both ASWP_FUPLOAD and ASWP_CAMUPLOAD need to be true, as ASWP_CAMUPLOAD depends on ASWP_FUPLOAD.

Then, you need to go inside MainActivity. Inside asw_view.setWebChromeClient() you should have these voids:

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,WebChromeClient.FileChooserParams fileChooserParams)

There is the logic for your photo management. First is compatible with Android API 16+, second one is with 21+.

Now, as of API 16+ (first method), I can't see a direct solution. You could use the one from this thread. The second method though is easily editable. You have if statements verifying ASWP_CAMUPLOAD and ASWP_FUPLOAD. At the end of ASWP_CAMUPLOAD verification, you have this:

if(ASWP_CAMUPLOAD) {
    //...
} else {
    intentArray = new Intent[0];
}

return false instead, so file upload itself should be cancelled, and camera upload will become an only solution.

C0nverter
  • 114
  • 13
  • Thanks for the reply! But I lost myself from "Now, as of API 16+..." What do I need to modify to solve my problem? I'm so sorry I'm not very strong in programming > – VentusZXC Jan 20 '20 at 13:50
  • Wait I kinda get it, so I should return false at the end of the ASWP_CAMUPLOAD loop right? – VentusZXC Jan 20 '20 at 13:56
  • `openFileChooser()` is your method for older Android versions. API 16 is probably around Android 4.x. You can try to understand the code here to see, how the file is retrieved, and use thread I've provided you, to modify this method. – C0nverter Jan 20 '20 at 13:57
  • Yes, but keep in mind, that this will work for Android API 21+. Devices with Android under Lollipop will still have a choice between file and camera. – C0nverter Jan 20 '20 at 13:58
  • ah alright, I'll have a read first and if I don't understand, is there any better way to contact you? – VentusZXC Jan 20 '20 at 14:10
  • Sure! You can write to me at `m.kramek@outlook.com`. – C0nverter Jan 20 '20 at 14:21