WebChromeClient onShowFileChooser()

时间:2015-08-12 20:54:30

标签: android android-intent android-webview onactivityresult filechooser

我在html中尝试输入文件标签,以便在我的WebView中使用它。我创建了一个选择器意图,因此用户可以选择是否要拍摄新照片或从照片库中选择现有照片。我的问题是在onActivityResult()中,结果代码是-1而不是0,无论从选择器中挑选什么,意图动作总是相同的。

这是我的代码:

@Override
    public boolean onShowFileChooser(
            WebView webView, ValueCallback<Uri[]> filePathCallback,
            WebChromeClient.FileChooserParams fileChooserParams) {

        Log.i(TAG, "onShowFileChooser()");

        this.mFilePathCallback = filePathCallback;

        final Intent chooserIntent = createChooserIntent();
        this.mainActivity.startActivityForResult(chooserIntent, MainActivity.FILECHOOSER_RESULTCODE);

        return true;
    }

    private Intent createChooserIntent() {
        final Intent imageCaptureIntent = this.createImageCaptureIntent();
        final Intent imagePickerIntent = this.createImagePickerIntent();

        final Intent imageChooserMenuIntent = Intent.createChooser(imagePickerIntent, "Image Chooser");
        imageChooserMenuIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[] {imageCaptureIntent});

        return imageChooserMenuIntent;
    }

    private Intent createImagePickerIntent() {
        final Intent imagePickerIntent = new Intent(Intent.ACTION_PICK);
        imagePickerIntent.setType("image/*");

        return imagePickerIntent;
    }

    private Intent createImageCaptureIntent() {
        final Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File file = new File(
                Environment.getExternalStorageDirectory().getPath() + File.separator + "Avatar.jpg");
        this.mCapturedImageURI = Uri.fromFile(file);
        imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, this.mCapturedImageURI);

        return imageCaptureIntent;
    }

onActivityResult():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.i(TAG, "onActivityResult(): " + requestCode + " " + resultCode);

    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (data.getAction() == MediaStore.ACTION_IMAGE_CAPTURE) {
            Toast.makeText(this, "FROM CAMERA", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "FROM PICKER", Toast.LENGTH_SHORT).show();
        }

        //TO DO: get the picture and pass it to onReceiveValue();

        this.webChromeClient.getmFilePathCallback().onReceiveValue(null);
    }
}

我做错了什么?你能救我吗?

0 个答案:

没有答案