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 (

相关内容

热门资讯

2026外挂!混沌休闲辅助,神... 2026外挂!混沌休闲辅助,神途辅助脚本(都是有挂)1、神途辅助脚本免费脚本咨询教程、神途辅助脚本免...
2026外挂!潮汕雀友会辅助,... 2026外挂!潮汕雀友会辅助,崇阳斗棋辅助脚本视频(果然有挂)1、点击下载安装,崇阳斗棋辅助脚本视频...
2026外挂!乐酷副厅外挂,宝... 2026外挂!乐酷副厅外挂,宝宝浙江辅助脚本(都是是有挂)1、玩家可以在宝宝浙江辅助脚本线上大神俱乐...
2026外挂!欢乐情怀挂,朋朋... 2026外挂!欢乐情怀挂,朋朋政和软件辅助器(其实真的有挂)1、下载好朋朋政和软件辅助器正确养号方法...
2026外挂!免费游戏辅助软件... 2026外挂!免费游戏辅助软件,多乐跑的快游戏辅助脚本(确实有挂)1、多乐跑的快游戏辅助脚本辅助软件...
2026外挂!打哈儿脚本,鸿狐... 2026外挂!打哈儿脚本,鸿狐拼十辅助(确实是真的有挂)1、鸿狐拼十辅助公共底牌简单,鸿狐拼十辅助透...
2026外挂!枫叶辅助官网地址... 2026外挂!枫叶辅助官网地址,微乐脚本辅助器(真是有挂)1、很好的工具软件,可以解锁游戏的枫叶辅助...
2026外挂!山西扣点免费辅助... 2026外挂!山西扣点免费辅助下载,微乐多乐跑作弊(果然是有挂)1、实时山西扣点免费辅助下载透视辅助...
2026外挂!堆堆乐辅助软件,... 2026外挂!堆堆乐辅助软件,山城九九辅助(原来是有挂)1、在堆堆乐辅助软件插件功能辅助器技巧中,中...
2026外挂!创思维辅助器,拼... 2026外挂!创思维辅助器,拼十app辅助(果然真的是有挂);1、完成创思维辅助器有辅助插件,帮助玩...