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.陕麻圈免费辅助工具 选牌...
反观!枫叶辅助官网地址!原来是... 反观!枫叶辅助官网地址!原来是有辅助平台(有挂详情)-哔哩哔哩进入游戏-大厅左侧-新手福利-激活码辅...
此事引发广泛关注!关春天辅助器... 此事引发广泛关注!关春天辅助器为什么收费!切实存在有辅助器(存在有挂)-哔哩哔哩1、游戏颠覆性的策略...
记者获悉!新天道怎么看底牌!竟... 记者获悉!新天道怎么看底牌!竟然是有辅助插件(有挂讲解)-哔哩哔哩1、玩家可以在新天道怎么看底牌线上...
有玩家发现!闲逸亲友圈下载辅助... 有玩家发现!闲逸亲友圈下载辅助!果然存在有辅助器(有挂总结)-哔哩哔哩1、玩家可以在闲逸亲友圈下载辅...
据通报!创思维激k辅助!确实真... 据通报!创思维激k辅助!确实真的是有辅助插件(有挂实锤)-哔哩哔哩1、在创思维激k辅助插件功能辅助器...
据通报!四川游戏家园破解版!原... 据通报!四川游戏家园破解版!原来有挂辅助工具(竟然有挂)-哔哩哔哩1、许多玩家不知道四川游戏家园破解...
受玩家影响!余音游戏决战十三水... 受玩家影响!余音游戏决战十三水!一直有挂辅助挂(真的有挂)-哔哩哔哩1、很好的工具软件,可以解锁游戏...
据公告内容!如何下载安装胡乐辅... 据公告内容!如何下载安装胡乐辅助脚本!切实真的是有辅助器(有挂秘笈)-哔哩哔哩1、金币登录送、破产送...
针对!越乡游义乌微信小程序辅助... 针对!越乡游义乌微信小程序辅助!总是有挂辅助神器(确实有挂)-哔哩哔哩1、进入到越乡游义乌微信小程序...