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游戏下... 解迷透视!wepoker游戏下载,微信小程序游戏破解微乐游戏,练习教程(有挂规律)-哔哩哔哩暗藏猫腻...
详细透视!德普之星辅助工具如何... 详细透视!德普之星辅助工具如何打开,微乐小程序黑科技免费知乎,课程教程(有挂技术)-哔哩哔哩1、许多...
总结透视!wepoker辅助器... 总结透视!wepoker辅助器,微信微乐小程序有窍门吗,秘籍教程(有挂秘籍)-哔哩哔哩1、wepok...
辅助透视!wpk透视是真的假的... 辅助透视!wpk透视是真的假的,微乐自建房脚本最新版本更新内容,指引教程(有挂功能)-哔哩哔哩1、w...
解谜透视!德州局透视脚本下载安... 解谜透视!德州局透视脚本下载安装最新版本,微乐小程序黑科技app入口,模块教程(有挂秘笈)-哔哩哔哩...
解密透视!hhpoker作弊实... 解密透视!hhpoker作弊实战视频,微乐老友广东辅助器,手筋教程(详细教程)-哔哩哔哩1、让任何用...
普及透视!poker辅助器免费... 普及透视!poker辅助器免费安装,微乐自建房免费脚本哪个最好用,秘籍教程(有挂头条)-哔哩哔哩1、...
揭露透视!aapoker公共底... 揭露透视!aapoker公共底牌,微乐春天小程序辅助,操作教程(证实有挂)-哔哩哔哩1、每一步都需要...
详细透视!aapoker怎么拿... 详细透视!aapoker怎么拿好牌,收集微乐小程序游戏破解器,讲义教程(有挂细节)-哔哩哔哩1、aa...
揭幕透视!wepoker脚本,... 揭幕透视!wepoker脚本,微信小程序游戏充值破解,模板教程(有挂秘籍)-哔哩哔哩1、wepoke...