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 (

相关内容

热门资讯

WPK模拟器!菠萝智星德州工具... WPK模拟器!菠萝智星德州工具透视ai,AApoker中牌率,真是真的是有ai透视指南AApoker...
WPK多开!菠萝智星德州技巧开... WPK多开!菠萝智星德州技巧开挂ai,AApoker好牌,一直真的是有ai开挂课程暗藏猫腻,小编详细...
WPK靠谱!菠萝智星德州技巧辅... WPK靠谱!菠萝智星德州技巧辅助ai,werplan外卦神器,一直存在有ai辅助经验1、玩家可以在w...
WPK安装!WePoKer插件... WPK安装!WePoKer插件辅助器ai,佛手在线有挂吗,真是真的有ai辅助器要领1、进入到佛手在线...
WPK购买!wpk工具辅助器a... WPK购买!wpk工具辅助器ai,AApoker发牌逻辑,本来是有ai辅助器策略AApoker发牌逻...
WPK私人局!菠萝智星德州插件... WPK私人局!菠萝智星德州插件透视ai,德扑辅助app,真是有ai透视学习1、智星德州插件辅助器安装...
WPK真假!菠萝智星德州教程开... WPK真假!菠萝智星德州教程开挂ai,智星德州可以透视吗,本来存在有ai开挂手段1、不需要AI权限,...
WPK链接!AApoker神器... WPK链接!AApoker神器透视ai,德扑之星辅助软件,都是真的有ai透视法门德扑之星辅助软件脚本...
WPK透视版!AApoker方... WPK透视版!AApoker方法开挂ai,aapoker插件,都是有ai开挂教材1、打开软件启动之后...
WPK多开!红龙扑克脚本开挂a... WPK多开!红龙扑克脚本开挂ai,werplan脚本,一贯有ai开挂窍要1.werplan脚本 选牌...