AndroidStudio中Webview中的文件上传按钮不起作用。
创始人
2024-08-21 06:30:25
0

1.添加以下代码到AndroidManifest.xml文件中的Webview activity中。

android:hardwareAccelerated="false"
android:allowFileAccess="true"
android:allowContentAccess="true"

2.在Webview的Client中覆写onShowFileChooser方法,示例如下:

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
    if (mFilePathCallback != null) {
        mFilePathCallback.onReceiveValue(null);
    }
    mFilePathCallback = filePathCallback;
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(webView.getContext().getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
            takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
        } catch (IOException ex) {
            Log.e(TAG, "Unable to create Image File", ex);
        }
        if (photoFile != null) {
            mCameraPhotoPath = "file:" + photoFile.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        } else {
            takePictureIntent = null;
        }
    }
    Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType("*/*");
    Intent[] intentArray;
    if (takePictureIntent != null) {
        intentArray = new Intent[]{takePictureIntent};
    } else {
        intentArray = new Intent[0];
    }
    Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "FileChooser");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
    startActivityForResult(chooserIntent, mRequestCodeFilePicker);
    return true;
}

3.在Webview的 activity 中添加以下 onActivityResult 方法代码。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == mRequestCodeFilePicker) {
        if (mFilePathCallback == null) {
            return;
        }
        if (

相关内容

热门资讯

黑科技辅助!WepOke透明挂... 黑科技辅助!WepOke透明挂透视挂靠谱,(wePOke)总是有挂,详细教程(有挂总结)1、任何we...
黑科技辅助!wePOKE透明挂... 黑科技辅助!wePOKE透明挂软件规律,(wEPoke)果然是真的有挂,透视教程(有挂软件);wEP...
黑科技辅助挂!(WEpoke)... 黑科技辅助挂!(WEpoke)总是真的有挂,(wEPoke)都是真的是有挂,科技教程(有挂辅助器);...
黑科技辅助挂!wpK透视辅助,... 黑科技辅助挂!wpK透视辅助,(wpK)原来真的是有挂,细节方法(有挂神器)1、很好的工具软件,可以...
黑科技辅助!WePoke透明挂... 黑科技辅助!WePoke透明挂模拟器,(WePoke)确实是有挂,黑科技教程(有挂插件)WePoke...
黑科技辅助挂!wEpoke软件... 黑科技辅助挂!wEpoke软件透明挂,wePokE透明挂可以识别,总结教程(有挂透明挂)运wePok...
黑科技辅助挂!WPK透视辅助,... 黑科技辅助挂!WPK透视辅助,(wPk)好像真的有挂,普及教程(有挂辅助挂)wPk软件透明挂微扑克w...
黑科技辅助挂!来玩德州辅助工具... 黑科技辅助挂!来玩德州辅助工具,(云扑克德州)果然存在有挂,插件教程(有挂科技)1、玩家可以在云扑克...
黑科技辅助挂!德州之星辅助工具... 黑科技辅助挂!德州之星辅助工具,(线上wpk德州)确实真的有挂,第三方教程(有挂方法)1、该软件可以...
黑科技辅助!wEpOke透明挂... 黑科技辅助!wEpOke透明挂有挂蓝鸟,(wepokE)切实真的是有挂,玩家教你(有挂教程)1、wE...