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 (

相关内容

热门资讯

透视ai代打"hhp... 1、透视ai代打"hhpoker到底可以作弊码"详细辅助AI教程(其实真的有挂);详细教程。2、hh...
技术分享!约局吧德州真的存在透... 技术分享!约局吧德州真的存在透视吗,红龙poker辅助器免费观看,解说技巧(有挂软件);玩家必备必赢...
透视线上"佛手大菠萝... 1、透视线上"佛手大菠萝辅助"详细辅助必赢方法(本来真的有挂)。2、佛手大菠萝辅助透视辅助简单,佛手...
科技新动态!wepoker辅助... 1、科技新动态!wepoker辅助器安装包,wepoker俱乐部辅助器,2025新版(有挂透明);详...
透视安卓版"wepo... 透视安卓版"wepoker买脚本靠谱吗"详细辅助切实教程(起初是真的有挂);致您一封信;亲爱wepo...
六分钟了解!德普软件,wepo... 六分钟了解!德普软件,wepoker私人局透视插件,可靠技巧(有挂透明);亲真的是有正版授权,小编(...
透视科技"pokem... 透视科技"pokemomo辅助软件"详细辅助新2025教程(其实真的有挂)准备好在pokemomo辅...
揭秘攻略!德普之星透视,德扑H... 揭秘攻略!德普之星透视,德扑HHpoker有挂吗,德州论坛(有挂技巧);玩家必备必赢加哟《13670...
透视最新"wepok... 此外,数据分析德州(wepoker开脚本视频)辅助神器app还具备辅助透视行为开挂功能,通过对客户w...
玩家必看科普!智星菠萝辅助,h... 玩家必看科普!智星菠萝辅助,hhpoker透视脚本下载,可靠教程(有挂软件);支持2-10人实时对战...