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 (

相关内容

热门资讯

第7分钟辅助!九九山城万州版脚... 第7分钟辅助!九九山城万州版脚本,一直真的有辅助攻略(有挂解惑)1、不需要AI权限,帮助你快速的进行...
两分钟辅助!丽水都莱辅助工具试... 两分钟辅助!丽水都莱辅助工具试用,果然存在有辅助插件(果真有挂)该软件可以轻松地帮助玩家将丽水都莱辅...
9分钟辅助!福建天天开心无限辅... 9分钟辅助!福建天天开心无限辅助科技,其实真的是有辅助教程(有挂分享)1、福建天天开心无限辅助科技破...
十分钟辅助!南宁友乐辅助,都是... 十分钟辅助!南宁友乐辅助,都是有辅助挂(有挂辅助)1、南宁友乐辅助辅助软件下载优化,南宁友乐辅助底牌...
八分钟辅助!奇迹陕西辅助,竟然... 八分钟辅助!奇迹陕西辅助,竟然是有辅助神器(有挂方式)1、实时奇迹陕西辅助透视辅助更新:用户可以随时...
第6分钟辅助!欢聚水鱼辅助插件... 第6分钟辅助!欢聚水鱼辅助插件,一贯存在有辅助技巧(有挂细节)暗藏猫腻,小编详细说明欢聚水鱼辅助插件...
八分钟辅助!新西部外卦辅助器,... 八分钟辅助!新西部外卦辅助器,都是真的有辅助器(有挂方法)1、新西部外卦辅助器公共底牌简单,新西部外...
两分钟辅助!微乐陕西小程序辅助... 两分钟辅助!微乐陕西小程序辅助,真是真的有辅助教程(有挂解惑)1、让任何用户在无需微乐陕西小程序辅助...
1分钟辅助!福建天天开心万能辅... 1分钟辅助!福建天天开心万能辅助,真是有辅助插件(有挂详细)1、实时福建天天开心万能辅助透视辅助更新...
第四分钟辅助!微信茶馆辅助器,... 第四分钟辅助!微信茶馆辅助器,竟然真的有辅助技巧(有挂助手)1、上手简单,内置详细流程视频教学,新手...