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 (

相关内容

热门资讯

免费测试版!陕西扣点点脚本辅助... 免费测试版!陕西扣点点脚本辅助器(辅助挂)wepoker私局辅助(从来真的是有挂);AI辅助机器人普...
专业讨论!皮皮辅助软件下载(辅... 专业讨论!皮皮辅助软件下载(辅助挂)pokemmo手机脚本(固有是真的有挂);1、很好的皮皮辅助软件...
揭秘几款!吉祥填大坑插件(辅助... 揭秘几款!吉祥填大坑插件(辅助挂)xpoker辅助怎么用(本然是真的有挂);是一款可以让一直输的玩家...
科普攻略!雀神挂件价格骗局(辅... 您好,雀神挂件价格骗局这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
分享个大家!开心泉州小程序工具... 分享个大家!开心泉州小程序工具(辅助挂)fishpoker透视(最初真的是有挂);开心泉州小程序工具...
最新技巧!金虎爷辅助插件(辅助... 最新技巧!金虎爷辅助插件(辅助挂)哈糖大菠萝万能挂(真是真的是有挂)相信很多朋友都在电脑上玩过金虎爷...
分享个大家!丽水都莱破解器(辅... 分享个大家!丽水都莱破解器(辅助挂)wepoker辅助器下载(最初真的是有挂);超受欢迎的丽水都莱破...
让我来分享经验!微信小程序游戏... 让我来分享经验!微信小程序游戏破解器(辅助挂)hhpoker是正品吗(一贯存在有挂);运微信小程序游...
传递经验!长城互娱辅助(辅助挂... 传递经验!长城互娱辅助(辅助挂)德普辅助器怎么用(都是真的是有挂);相信小伙伴都知道这个长城互娱辅助...
专业讨论!陕西辅助(辅助挂)德... 专业讨论!陕西辅助(辅助挂)德州透视插件(最初是有挂);陕西辅助是一款益智类棋牌手游,可以说是伴随了...