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 (

相关内容

热门资讯

解密关于!皮皮跑得快透视挂功能... 解密关于!皮皮跑得快透视挂功能神器,皮皮跑得快竟然是有透视挂神器(有辅助挂解惑)1、皮皮跑得快免费脚...
程序员教你!悠味竞技辅助功能神... 程序员教你!悠味竞技辅助功能神器,悠味竞技确实是有辅助神器(有辅助挂规律)1、悠味竞技模拟器是什么优...
我来分享!仲乐麻将透视功能工具... 我来分享!仲乐麻将透视功能工具,仲乐麻将确实有透视工具(有辅助挂存在)1、玩家可以在仲乐麻将线上大神...
我来向大家传授!新大海互娱辅助... 我来向大家传授!新大海互娱辅助功能软件,新大海互娱真是真的有辅助软件(有辅助挂神器)1)新大海互娱有...
玩家攻略推荐!锋视棋牌透视挂功... 玩家攻略推荐!锋视棋牌透视挂功能软件,锋视棋牌总是真的有透视挂软件(有辅助挂辅助)1、锋视棋牌免费辅...
玩家必备攻略!圣游透视功能技巧... 玩家必备攻略!圣游透视功能技巧,圣游本来是有透视技巧(有辅助挂辅教程)1、圣游破解器简单,圣游机器人...
推荐一款!青龙区辅助功能技巧,... 推荐一款!青龙区辅助功能技巧,青龙区本来有辅助技巧(有辅助挂猫腻)1、不需要AI权限,帮助你快速的进...
一秒答解!粤东百乐汇辅助器功能... 一秒答解!粤东百乐汇辅助器功能插件,粤东百乐汇好像真的有辅助器插件(有辅助挂教学)1、在粤东百乐汇插...
玩家攻略!边锋干瞪眼辅助器功能... 玩家攻略!边锋干瞪眼辅助器功能app,边锋干瞪眼一贯是真的有辅助器app(有辅助挂秘诀)边锋干瞪眼透...
玩家必备科普!双扣游戏透视功能... 玩家必备科普!双扣游戏透视功能技巧,双扣游戏一直是真的有透视技巧(有辅助挂攻略)1、点击下载安装,双...