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 时,都担心wepoker透视有没有、内部资料上传带来隐私风险。这款本地运行的 ...
专业讨论!辅助挂!wepoke... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
记者爆料开挂!德普之星透视辅助... 记者爆料开挂!德普之星透视辅助插件,wepoker开挂技巧插件(竟然真的是有开挂)主打本地运行的 A...
一分钟带你了解透视挂!we p... 您好:we poker辅助器下载这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
一分钟教你辅助器!wpk俱乐部... 很多人使用云端 AI 时,都担心wpk俱乐部是真的吗、内部资料上传带来隐私风险。这款本地运行的 AI...
科普透视!智星德州插件最新版本... 科普透视!智星德州插件最新版本更新内容详解,微扑克透视软件下载教程(最初有透视)AI 是一款可以让一...
最新技巧!辅助挂!wepoke... 最新技巧!辅助挂!wepoker破解是真的还是假的,德扑之星开挂工具脚本(真是是有辅助挂)主打本地运...
玩家实测透视!wpk辅助器,W... 玩家实测透视!wpk辅助器,WPK透视辅助神器(先前是真的有透视)AI 智能体黑科技再上新,自动化能...
一分钟教你开挂!wepoker... 您好,一分钟教你开挂!wepoker私人局外卦,德普之星是如何开挂的插件(从来有开挂)这款游戏可以开...
推荐十款辅助!wepoker透... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...