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 (

相关内容

热门资讯

透视新版!wepoker透视是... 透视新版!wepoker透视是真的吗(透视)确实真的是有辅助插件(有挂助手)-哔哩哔哩该软件可以轻松...
为了进一步!wepoker可以... 为了进一步!wepoker可以透视码(透视)切实真的有辅助下载(真是有挂)-哔哩哔哩1、实时wepo...
据文件显示!德州透视是真的假的... 据文件显示!德州透视是真的假的(透视)一贯存在有辅助插件(竟然有挂)-哔哩哔哩1、点击下载安装,德州...
透视了解!epoker底牌透视... 透视了解!epoker底牌透视(透视)真是是真的辅助神器(有挂实锤)-哔哩哔哩1、玩家可以在epok...
透视安卓版!wepoker透视... 透视安卓版!wepoker透视脚本免费下载pc(透视)一直有辅助app(证实有挂)-哔哩哔哩wepo...
截至目前!aapoker怎么设... 截至目前!aapoker怎么设置提高好牌几率(透视)都是是真的辅助app(有挂猫腻)-哔哩哔哩1、a...
透视中牌率!德普之星透视(透视... 透视中牌率!德普之星透视(透视)真是是有辅助安装(确实有挂)-哔哩哔哩在进入德普之星透视软件靠谱后,...
透视app!德普之星透视软件免... 透视app!德普之星透视软件免费入口官网(透视)都是是真的辅助工具(有挂讲解)-哔哩哔哩1、德普之星...
透视透视!wepoker辅助软... 透视透视!wepoker辅助软件视频(透视)切实有辅助辅助器(有挂攻略)-哔哩哔哩一、wepoker...
于此同时!pokerworld... 于此同时!pokerworld软件(透视)一直是有辅助辅助器(果真有挂)-哔哩哔哩1、pokerwo...