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 (

相关内容

热门资讯

wepokeai机器人!wep... wepokeai机器人!wepokeai辅助(Wepoke检测)一直是有挂(详细ai代打的胜率教程)...
aapoker辅助工具!aap... aapoker辅助工具!aapoker辅助软件,(aapoker系统)一直是真的有挂(详细辅助工具教...
微扑克游戏辅助器!微扑克插件,... 1、微扑克游戏辅助器!微扑克插件,(微扑克有挂)果然真的有挂(详细辅助器ios教程);详细教程。2、...
德扑之星实战!德扑之星里面怎么... 德扑之星实战!德扑之星里面怎么隐藏入池率,德扑安卓都是真的有挂(详细ai购买教程) 科技详细教程;7...
wepoke辅助技巧!wepo... wepoke辅助技巧!wepoke科技(wepoke实锤)都是是真的有挂(详细有外挂教程);科技安装...
红龙扑克辅助器!红龙扑克是不是... 红龙扑克辅助器!红龙扑克是不是有问题,(红龙扑克)真是存在有挂(详细辅助工具教程);软件透明挂作为一...
德州之星插件!德扑之星为什么不... 德州之星插件!德扑之星为什么不发牌,德扑之星功能确实有挂(详细ai代打教程);原来确实真的有挂(需添...
微扑克有辅助挂!微扑克系统发牌... 1、微扑克有辅助挂!微扑克系统发牌规律,(微扑克钻石)真是存在有挂(详细德州专用辅助器教程);详细教...
德扑ai智能机器人!德扑之星 ... 德扑ai智能机器人!德扑之星 ai软件,德扑之星代打好像是真的有挂(详细网上教程);玩家在德扑ai智...
德扑之星代打!德扑之星为什么不... 德扑之星代打!德扑之星为什么不发牌,德扑手机上一贯存在有挂(详细手机上教程);(需添加指定薇7574...