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 (

相关内容

热门资讯

总算了解!九游破解版真的假的!... 总算了解!九游破解版真的假的!其实真的有辅助教程(有挂功能)九游破解版真的假的能透视中分为三种模型:...
重大通报!雀友会广东潮汕麻雀万... 重大通报!雀友会广东潮汕麻雀万能辅助器!总是存在有辅助app(有挂技术)1、点击下载安装,雀友会广东...
盘点几款!微乐江苏自建房辅助a... 盘点几款!微乐江苏自建房辅助app!好像真的有辅助app(有挂存在)1、在微乐江苏自建房辅助app插...
一分钟教会你!上品斗地主游戏辅... 一分钟教会你!上品斗地主游戏辅助!真是确实有辅助软件(有挂头条)1、上品斗地主游戏辅助免费辅助多个强...
一分钟揭秘!点点长牌源码!真是... 一分钟揭秘!点点长牌源码!真是存在有辅助挂(有挂教程)1、点点长牌源码免费脚本咨询教程、点点长牌源码...
这一现象值得深思!广东闲来辅助... 这一现象值得深思!广东闲来辅助免费!原来存在有辅助软件(有挂实锤)进入游戏-大厅左侧-新手福利-激活...
玩家必看分享!万能脚本辅助器!... 玩家必看分享!万能脚本辅助器!确实确实有辅助挂(有挂秘笈)万能脚本辅助器破解侠是真的助透视。每个模型...
2024教程!随意玩房卡辅助!... 2024教程!随意玩房卡辅助!原来真的有辅助技巧(发现有挂)1、让任何用户在无需随意玩房卡辅助安装教...
技术分享!大头大菠萝辅助!都是... 技术分享!大头大菠萝辅助!都是是真的有辅助技巧(有挂总结)1、起透看视 大头大菠萝辅助辅助软件价格2...
围绕透视问题!菠萝辅助破解版!... 围绕透视问题!菠萝辅助破解版!一贯真的是有辅助攻略(有挂分享)围绕透视问题!菠萝辅助破解版!一贯真的...