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 (

相关内容

热门资讯

揭秘攻略!蜀山四川麻亲友房祈福... 揭秘攻略!蜀山四川麻亲友房祈福,微乐a3纸牌有脚本,详细脚本(有挂秘诀)1、每一步都需要思考,不同水...
黑科技技巧!wepoker破解... 黑科技技巧!wepoker破解器有用吗,德普之星辅助器,本来是真的有挂(证实有挂)1、金币登录送、破...
2分钟细说!创思维激k有辅助器... 2分钟细说!创思维激k有辅助器吗,闲来辅助软件怎么样,详细神器(了解有挂)运创思维激k有辅助器吗辅助...
刚刚!hhpoker透视脚本视... 刚刚!hhpoker透视脚本视频,德扑之星透视免费,确实是有挂(有人有挂)亲,关键说明,德扑之星透视...
推荐攻略!微信小程序雀神广东智... 推荐攻略!微信小程序雀神广东智能辅助,潮友会鱼虾蟹破解,详细攻略(有挂详细)1、微信小程序雀神广东智...
有消息称!wepoker数据分... 有消息称!wepoker数据分析工具,德普之星私人局透视,都是真的有挂(有挂实锤)1、进入游戏-大厅...
重磅来袭!乐酷解码辅助,山西扣... 重磅来袭!乐酷解码辅助,山西扣点点辅助工具,详细脚本(新版有挂)1、超多福利:超高返利,海量正版游戏...
第三方技巧!aapoker如何... 您好,德扑之星透视辅助软件激活码这款游戏可以开挂的,确实是有挂的,需要了解加去威信【13670430...
记者发布!微乐广西麻辣辅助,大... 记者发布!微乐广西麻辣辅助,大发棋牌辅助,详细工具(有挂解惑)1、该软件可以轻松地帮助玩家将微乐广西...
据目击者称!hhpoker作弊... 据目击者称!hhpoker作弊码怎么用,德扑之星私人局辅助器,切实是有挂(揭秘有挂)德扑之星私人局辅...