Android Webview中的照片/文档上传

时间:2016-07-01 13:52:44

标签: android webview camera gallery

我正在开发Android的包装应用程序并需要连接到相机/照片库(因此用户可以上传自己的照片)。我已经找到了关于此主题的一些帖子,包括Upload an Image from camera or gallery in WebViewUpload camera photo and filechooser from webview INPUT field,但我在这些解决方案之后建模的代码并不起作用。我在openFileChooser函数中放置了一个print语句,当我在webview中点击添加照片/文档Javascript链接时,它没有被触发。任何建议将不胜感激,我在下面包括我的代码:

 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;
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎打开了startActivityForResult结果代码REQUEST_PHOTOS,但在onActivityResult中,您检查了代码REQUEST_PHOTOSREQUEST_CAMERA。所以你永远不会处理结果。 使用以下内容更改onActivityResult

@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;
        }
    }