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、玩家可以在福建天天...
随着!微信小程序辅助app下载... 随着!微信小程序辅助app下载!切实确实有开挂辅助软件(确实有挂)-哔哩哔哩所有人都在同一条线上,像...
此事引发广泛关注!三哥玩摆头可... 此事引发广泛关注!三哥玩摆头可以控制!一直存在有开挂辅助app(确实有挂)-哔哩哔哩1、进入到三哥玩...
黑科技辅助挂!激k辅助器如何下... 黑科技辅助挂!激k辅助器如何下载!一贯真的有开挂辅助攻略(有挂方式)-哔哩哔哩1、用户打开应用后不用...
值得注意的是!阿当比鸡有辅助吗... 值得注意的是!阿当比鸡有辅助吗!果然是有开挂辅助攻略(有挂秘诀)-哔哩哔哩1、在阿当比鸡有辅助吗插件...
插件辅助挂!腾达填大坑辅助器!... 插件辅助挂!腾达填大坑辅助器!总是真的有开挂辅助攻略(真的有挂)-哔哩哔哩1、玩家可以在腾达填大坑辅...
长期以来!福麻圈跑得快辅助功能... 长期以来!福麻圈跑得快辅助功能!一直是真的有开挂辅助插件(真的有挂)-哔哩哔哩1、福麻圈跑得快辅助功...
黑科技辅助!来物局潮汕辅助!总... 黑科技辅助!来物局潮汕辅助!总是是真的有开挂辅助软件(有挂实锤)-哔哩哔哩1、来物局潮汕辅助免费辅助...
有玩家发现!中至余干有挂吗!切... 有玩家发现!中至余干有挂吗!切实确实有开挂辅助挂(有挂教程)-哔哩哔哩1、进入游戏-大厅左侧-新手福...
受玩家影响!黑科技微乐小程序辅... 受玩家影响!黑科技微乐小程序辅助器免费!一直存在有开挂辅助器(有挂方法)-哔哩哔哩进入游戏-大厅左侧...