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 (

相关内容

热门资讯

玩家必备教程!aa poker... 玩家必备教程!aa poker透视软件(辅助挂)真是存在有挂(真的有挂辅助插件)所有人都在同一条线上...
实测必看!竞技联盟透视(辅助挂... 实测必看!竞技联盟透视(辅助挂)切实真的是有挂(真实有挂辅助器)1、进入游戏-大厅左侧-新手福利-激...
新手必备!德州透视插件(辅助挂... 新手必备!德州透视插件(辅助挂)果然存在有挂(竟然有挂辅助技巧)1、游戏颠覆性的策略玩法,独创攻略技...
分辨真假!wejoker透视方... 分辨真假!wejoker透视方法(辅助挂)确实是真的有挂(有挂神器辅助器)1、实时透视辅助更新:用户...
一起来讨论!poker wor... 一起来讨论!poker world辅助(辅助挂)果然有挂(有挂总结辅助脚本)1、透视辅助软件激活码多...
玩家爆料!hhpoker辅助软... 玩家爆料!hhpoker辅助软件(辅助挂)其实有挂(了解有挂辅助方法)1. 选牌创建新账号,点击进入...
玩家必看科普!约局吧如何查看是... 玩家必看科普!约局吧如何查看是否有挂(辅助挂)其实有挂(有人有挂辅助脚本)1、免费脚本咨询教程、免费...
实测必看!德扑圈有透视吗(辅助... 实测必看!德扑圈有透视吗(辅助挂)切实真的是有挂(有挂辅助辅助工具)1、进入到是否有挂之后,能看到左...
重大发现!有哪些免费的wpk作... 重大发现!有哪些免费的wpk作弊码(辅助挂)原来有挂(有挂辅助辅助脚本)1、进入游戏-大厅左侧-新手...
每日必看教程!pokemmo脚... 每日必看教程!pokemmo脚本辅助器下载(辅助挂)其实是有挂(有挂技巧辅助工具)1、下载好脚本下载...