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 (

相关内容

热门资讯

2分钟细说!wepoker辅助... 2分钟细说!wepoker辅助器是真的吗,竞技联盟辅助插件,都是是有挂(有挂秘籍)1、这是跨平台的w...
科技新动态!wepoker怎么... 科技新动态!wepoker怎么设置盖牌,wepoker脚本下载,一直是真的有挂(有挂分享)1、让任何...
传递经验!we-poker正规... 传递经验!we-poker正规吗,pokernow辅助工具,总是存在有挂(有挂实锤)1、金币登录送、...
揭秘!aapoker辅助怎么用... 揭秘!aapoker辅助怎么用,德州局HHpoker透视脚本,其实是有挂(有挂教学)1)辅助插件:进...
玩家攻略!wepoker透视a... 玩家攻略!wepoker透视app下载,hhpoker德州挂真的有吗,真是是有挂(真实有挂)1、下载...
重大发现!大菠萝789辅助器下... 重大发现!大菠萝789辅助器下载,hhpoker辅助软件,一直是真的有挂(有挂讲解)1、起透看视 辅...
实操分享!wepoker插件下... 实操分享!wepoker插件下载,wepoker怎么发冤家牌,都是真的是有挂(有挂分享)1、辅助软件...
重要通知!wepoker分析,... 重要通知!wepoker分析,德普之星辅助器怎么用,一贯存在有挂(真的有挂)运德普之星辅助器辅助工具...
推荐攻略!wpk有那种辅助吗,... 推荐攻略!wpk有那种辅助吗,hhpoker有没有外挂,真是存在有挂(有挂总结)1、有没有辅助教程、...
发现玩家!约局吧德州真的存在透... 发现玩家!约局吧德州真的存在透视吗,aa poker辅助,都是是真的有挂(有挂教程)1、玩家可以在透...