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 (

相关内容

热门资讯

玩家必备科技!宝宝吃吃吃辅助a... 玩家必备科技!宝宝吃吃吃辅助ai透视挂app,宝宝吃吃吃其实是有透视器(有辅助挂规律)1、宝宝吃吃吃...
揭秘关于!华谊游戏辅助ai透视... 揭秘关于!华谊游戏辅助ai透视挂技巧,华谊游戏其实有开挂(有辅助挂分享);1、完成华谊游戏辅助器v3...
一分钟揭秘!响水麻将辅助ai透... 一分钟揭秘!响水麻将辅助ai透视挂工具,响水麻将总是存在有开挂(有辅助挂辅教程)1.响水麻将 选牌创...
程序员教你!星火桌游辅助ai透... 程序员教你!星火桌游辅助ai透视挂脚本,星火桌游确实真的有外挂(有辅助挂助手)星火桌游是不是有人用挂...
揭秘一下!新丫丫玉溪麻将辅助a... 揭秘一下!新丫丫玉溪麻将辅助ai透视挂工具,新丫丫玉溪麻将其实是有透视挂(有辅助挂解密)1、新丫丫玉...
实测发现!忻州天天麻将辅助ai... 实测发现!忻州天天麻将辅助ai透视挂工具,忻州天天麻将一直是有开挂(有辅助挂总结)1、每一步都需要思...
一分钟快速了解!琼戏互娱辅助a... 一分钟快速了解!琼戏互娱辅助ai透视挂软件,琼戏互娱原来真的是有作弊器(有辅助挂规律)1)琼戏互娱免...
技术分享!红中麻将辅助ai透视... 技术分享!红中麻将辅助ai透视挂脚本,红中麻将都是真的有辅助器(有辅助挂工具)红中麻将能透视中分为三...
揭秘关于!亲友房辅助ai透视挂... 揭秘关于!亲友房辅助ai透视挂方法,亲友房总是是有辅助器(有辅助挂技巧)1、亲友房免费脚本咨询教程、...
玩家科普!鄱阳翻精辅助ai透视... 玩家科普!鄱阳翻精辅助ai透视挂脚本,鄱阳翻精原来是有开挂(有辅助挂方针)鄱阳翻精破解侠是真的助透视...