0

I'm working on a wrapper application for Android and need to connect to the camera/photo gallery (so the user can upload a picture of themselves for example). I've found a number of posts on the subject, including Upload an Image from camera or gallery in WebView and Upload camera photo and filechooser from webview INPUT field, but the code that I've modeled after these solutions isn't working. I put a print statement inside the openFileChooser function, and it isn't being triggered when the add photo/document Javascript link is hit in my webview. Any suggestions would be appreciated, I'm including my code below:

 private Uri mCapturedImageURI = null;

//make HTML upload button work in Webview
private ValueCallback<Uri> mUploadMessage;
private String filePath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_log_in_page);
    Intent currentIntent = getIntent();
    String action = currentIntent.getAction();
    String url;

    //check for permissions
    boolean camera = checkCameraPermissions();
    boolean location = checkLocationPermissions();
    boolean photos = checkPhotoPermissions();


    if (!location){
        ActivityCompat.requestPermissions(this,
                new String[] {"android.permission.ACCESS_FINE_LOCATION"}, REQUEST_LOCATION);
        requested_location = true;
    }


    if (!camera ) {
        ActivityCompat.requestPermissions(this,
                new String[] {"android.permission.CAMERA"}, REQUEST_CAMERA);
        requested_camera = true;
    }


    if (!photos) {
        ActivityCompat.requestPermissions(this,
                new String[] {"android.permission.READ_EXTERNAL_STORAGE"}, REQUEST_PHOTOS);
        requested_photos = true;
    }

    url =  Constants.login_page;

    //start up the webview, initiate navigation client
    myWebView = (WebView) findViewById(R.id.webview);
    if (myWebView != null) {
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setDomStorageEnabled(true);
        myWebView.getSettings().setAllowFileAccess(true);
        myWebView.getSettings().setAllowFileAccessFromFileURLs(true);
        myWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        myWebView.getSettings().setSupportMultipleWindows(true);
        myWebView.getSettings().setGeolocationEnabled(true);
        myWebView.setWebChromeClient(new WebChromeClient() {

            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
                callback.invoke(origin, true, false);
            }

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType){
                this.openFileChooser(uploadMsg);
            }

            @SuppressWarnings("unused")
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String AcceptType, String capture) {
                this.openFileChooser(uploadMsg);
            }

            private Uri imageUri;

            public void openFileChooser(ValueCallback<Uri> uploadMsg ) {
                // Update message
                mUploadMessage = uploadMsg;

                System.out.println("we are here");

                File imageStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "my_app");
                filePath = imageStorageDir + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
                File file = new File(filePath);

                Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                LogInPage.this.startActivityForResult(captureIntent, REQUEST_PHOTOS);
            }

            });
        //myWebView.addJavascriptInterface(new MyJavascriptInterface(this), "Android");
        myWebView.loadUrl(url);
        myWebView.setWebViewClient(new NavigationClient());
    }
}




   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if ((requestCode == SELECT_PHOTO) ||(requestCode == REQUEST_CAMERA)) {
        if (null == this.mUploadMessage || (resultCode != RESULT_OK && !new File(filePath).exists())) {
            this.mUploadMessage.onReceiveValue(null);
        } else {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Images.Media.DATA, this.filePath);
            this.mUploadMessage.onReceiveValue(this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
        }
        this.mUploadMessage = null;
    }
}
Community
  • 1
  • 1
Ethan
  • 103
  • 1
  • 9

1 Answers1

0

It looks like you are opening a startActivityForResult with result code REQUEST_PHOTOS, but in onActivityResult you check for codes REQUEST_PHOTOS or REQUEST_CAMERA. So you are never going to process result. Change your onActivityResult with:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if ((requestCode == REQUEST_PHOTOS) ) {
            if (null == this.mUploadMessage || (resultCode != RESULT_OK && !new File(filePath).exists())) {
                this.mUploadMessage.onReceiveValue(null);
            } else {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, this.filePath);
                this.mUploadMessage.onReceiveValue(getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values));
            }
            this.mUploadMessage = null;
        }
    }