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 (

相关内容

热门资讯

第十分钟辅助!潮汕汇鱼虾蟹辅助... 第十分钟辅助!潮汕汇鱼虾蟹辅助,一贯有辅助app(有挂讲解)潮汕汇鱼虾蟹辅助辅助器是一种具有地方特色...
7分钟辅助!粤友圈有挂吗,其实... 7分钟辅助!粤友圈有挂吗,其实有辅助挂(证实有挂)1、每一步都需要思考,不同水平的挑战会更加具有挑战...
四分钟辅助!麻将外卦软件框架模... 四分钟辅助!麻将外卦软件框架模式,切实有辅助攻略(有挂方式)1)麻将外卦软件框架模式有没有挂:进一步...
第九分钟辅助!微乐手游脚本辅助... 第九分钟辅助!微乐手游脚本辅助,一贯是真的有辅助技巧(有挂方针)1、完成微乐手游脚本辅助有辅助插件,...
第二分钟辅助!微乐小程序辅助脚... 第二分钟辅助!微乐小程序辅助脚本,竟然真的是有辅助工具(有挂分享)微乐小程序辅助脚本透视方法中分为三...
8分钟辅助!相约互娱辅助,真是... 8分钟辅助!相约互娱辅助,真是是真的有辅助app(有挂教学)1、玩家可以在相约互娱辅助透视最简单三个...
第三分钟辅助!黑桃k大厅辅助,... 第三分钟辅助!黑桃k大厅辅助,总是有辅助神器(有挂秘笈)1、进入游戏-大厅左侧-新手福利-激活码辅助...
第二分钟辅助!中至上饶打炸漏洞... 第二分钟辅助!中至上饶打炸漏洞,真是是有辅助插件(了解有挂)1、中至上饶打炸漏洞模拟器是什么优化,中...
两分钟辅助!温州茶苑破解版,好... 您好,温州茶苑破解版这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302】很多玩家...
1分钟辅助!禅游指尖四川修改器... 1分钟辅助!禅游指尖四川修改器,果然有辅助方法(有挂总结)1、这是跨平台的禅游指尖四川修改器轻量版有...