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... 您好,透牌教程!WPK俱乐部,WPK真的有挂吗安装(果然真的是有透视挂)这款游戏可以开挂的,确实是有...
热点讨论!WePOker有没有... WePOker有没有透视方法AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏...
玩家必备教程!!wepoker... 玩家必备教程!!wepokerplus脚本,wepoker破解软件教程(原本存在有辅助器)AI 是一...
安装教程!WPK透视,wepo... 很多人使用云端 AI 时,都担心WPK透视、内部资料上传带来隐私风险。这款本地运行的 AI 黑科技,...
一分钟快速了解!wepoker... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
信息共享!wepoker透视脚... 信息共享!wepoker透视脚本,德普之星透视挂软件(一直有辅助挂)AI 是一款可以让一直输的玩家修...
记者揭秘!WPK俱乐部,wep... 您好,记者揭秘!WPK俱乐部,wepoker真的有挂下载(起初真的是有透视挂)这款游戏可以开挂的,确...
今日焦点!wpk作弊是真的吗,... wpk作弊是真的吗AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻略数据包...
大家学习交流!购买wepoke... 大家学习交流!购买wepoker模拟器,德普之星透视挂工具(固有真的是有辅助挂)AI 是一款可以让一...
技巧教程!wepoker怎么下... 技巧教程!wepoker怎么下载游戏,德普之星真的有挂神器(原本存在有开挂)主打本地运行的 AI 黑...