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攻略透视ai... WPK安卓!wpk攻略透视ai,aapoker真的假的,一直存在有ai透视机巧1、每一步都需要思考,...
WPK私人局!微扑克app透视... WPK私人局!微扑克app透视ai,佛手在线大菠萝辅助,切实有ai透视阶段一、佛手在线大菠萝辅助游戏...
WPK链接!wpk工具辅助器a... WPK链接!wpk工具辅助器ai,aapoker辅助软件合法吗,都是存在有ai辅助器教程书1、在aa...
WPK脚本!aapoker插件... WPK脚本!aapoker插件开挂ai,哈糖大菠萝怎么开挂,真是是有ai开挂指南哈糖大菠萝怎么开挂能...
WPK多开!德扑之星插件辅助a... WPK多开!德扑之星插件辅助ai,德普之星的辅助工具介绍,本来有ai辅助要领1、德普之星的辅助工具介...
WPK控制牌!aapoker脚... WPK控制牌!aapoker脚本辅助ai,德州机器人代打脚本,好像真的是有ai辅助演示1)德州机器人...
WPK靠谱!wpk方法透视挂a... WPK靠谱!wpk方法透视挂ai,we poker免费辅助器,一直真的有ai透视挂操作1、we po...
WPK购买!WPK软件开挂ai... WPK购买!WPK软件开挂ai,aapoker透视怎么用,真是真的是有ai开挂法门1、许多玩家不知道...
WPK作弊!aapoker插件... WPK作弊!aapoker插件辅助器ai,约局吧游戏挂,本来存在有ai辅助器攻略1、约局吧游戏挂免费...
WPK链接!WePoKer插件... WPK链接!WePoKer插件透视挂ai,约局吧德州透视,其实存在有ai透视挂模板1、进入到约局吧德...