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 (

相关内容

热门资讯

实测分享!智星菠萝德州辅助挂多... 实测分享!智星菠萝德州辅助挂多功能透视工具,原来是有挂(有挂细节)1、智星菠萝德州免费辅助多个强度级...
必备攻略!WPK辅助挂多功能透... 必备攻略!WPK辅助挂多功能透视工具,一直存在有挂(有挂攻略)WPK辅助器是一种具有地方特色的麻将游...
技术分享!红龙poker辅助挂... 技术分享!红龙poker辅助挂多功能透视工具,一贯是真的有挂(有挂方法)1、红龙poker辅助软件下...
我来教大家!WPK辅助挂多功能... 我来教大家!WPK辅助挂多功能透视工具,切实真的有挂(的确有挂)1、任何WPK透视是真的假的的玩家都...
技巧知识分享!智星德州扑克辅助... 技巧知识分享!智星德州扑克辅助挂多功能透视工具,本来存在有挂(有挂方法)1、智星德州扑克公共底牌简单...
玩家必用!微扑克辅助挂多功能透... 玩家必用!微扑克辅助挂多功能透视工具,本来是有挂(有挂讲解)1、不需要AI权限,帮助你快速的进行微扑...
盘点十款!微扑克辅助挂多功能透... 盘点十款!微扑克辅助挂多功能透视工具,果然是有挂(有挂分享)盘点十款!微扑克辅助挂多功能透视工具,果...
查到实测!WPK辅助挂多功能透... 查到实测!WPK辅助挂多功能透视工具,总是真的有挂(有挂秘籍)WPK透视方法中分为三种模型:WPK靠...
实测发现!红龙poker辅助挂... 实测发现!红龙poker辅助挂多功能透视工具,切实存在有挂(竟然有挂)1、该软件可以轻松地帮助玩家将...
盘点十款!WPK辅助挂多功能透... 盘点十款!WPK辅助挂多功能透视工具,本来存在有挂(有挂讲解)1、下载好WPK透视辅助下载之后点击打...