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 (

相关内容

热门资讯

透视模拟器!wpk软件是真的吗... 透视模拟器!wpk软件是真的吗,陕西微乐挖坑小程序脚本,有挂透视(揭秘有挂)-哔哩哔哩进入游戏-大厅...
透视工具!uupoker有透视... 透视工具!uupoker有透视吗,微信小程序微乐破解器2024,推荐透视(有挂头条)-哔哩哔哩1、玩...
透视阶段!wejoker辅助软... 透视阶段!wejoker辅助软件价格,WePoKer咨询好像存在有安装,第八分钟教程(发现有挂)1、...
脚本普及!wepoker辅助器... 脚本普及!wepoker辅助器官方,wepoker怎么挂飞机(透视)竟然是真的技巧(哔哩哔哩)透视方...
透视脚本!wpk私人辅助,微信... 透视脚本!wpk私人辅助,微信微乐游戏辅助脚本,详细脚本(有挂功能)-哔哩哔哩1、wpk私人辅助辅助...
透视脚本!hhpoker可以开... 透视脚本!hhpoker可以开挂吗,微乐自建房辅助工具2025在哪,辅助透视(真的有挂)-哔哩哔哩1...
脚本教你!wpk模拟器是什么,... 脚本教你!wpk模拟器是什么,wpk真的有透视嘛(透视)切实是有挂(哔哩哔哩)一、可以开透视的定义与...
透视秘籍!wpk软件是真的吗,... 透视秘籍!wpk软件是真的吗,WPK开挂总是存在有透视,八分钟教程(有挂工具)1、wpk软件是真的吗...
透视有挂!德州局hhpoker... 透视有挂!德州局hhpoker,hhpoker有没有作必弊挂(透视)本来真的有挂(哔哩哔哩)一、游戏...
相较于以往!wepoker究竟... 相较于以往!wepoker究竟有没有透视,微乐家乡自建房辅助app,关于脚本(果真有挂)-哔哩哔哩w...