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 (

相关内容

热门资讯

黑科技好友(扑克世界app)外... 黑科技好友(扑克世界app)外挂透明挂辅助神器(ai辅助)果然真的是有挂(有挂总结)-哔哩哔哩1、扑...
黑科技好牌(推扑克)外挂透明挂... 黑科技好牌(推扑克)外挂透明挂辅助代打(智能ai)一直真的有挂(有挂方法)-哔哩哔哩推扑克是一种具有...
黑科技规律(wpk作弊)外挂透... 黑科技规律(wpk作弊)外挂透明挂辅助挂(黑科技ai)果然真的是有挂(有挂工具)-哔哩哔哩;1、点击...
黑科技挂黑科技(pokerwo... 黑科技挂黑科技(pokerworld)外挂透明挂辅助下载(ai代打)原来是有挂(竟然有挂)-哔哩哔哩...
黑科技神器(鱼扑克app)外挂... 黑科技神器(鱼扑克app)外挂透明挂辅助代打(透明黑科技)一直存在有挂(真的有挂)-哔哩哔哩1、鱼扑...
黑科技规律(聚星扑克德州)外挂... 黑科技规律(聚星扑克德州)外挂透明挂辅助下载(透明黑科技)其实是真的有挂(存在有挂)-哔哩哔哩1、操...
黑科技实锤(wpk下载)外挂透... 黑科技实锤(wpk下载)外挂透明挂辅助神器(透明挂)竟然是真的有挂(新版有挂)-哔哩哔哩1、上手简单...
黑科技工具(wpk稳赢)外挂透... 黑科技工具(wpk稳赢)外挂透明挂辅助设备(ai辅助)一直存在有挂(真是有挂)-哔哩哔哩;1、操作简...
辅助黑科技(wpk大厅)外挂透... 辅助黑科技(wpk大厅)外挂透明挂辅助插件(ai代打)好像是有挂(有挂存在)-哔哩哔哩1、wpk大厅...
黑科技智能ai(wpk安装)外... 黑科技智能ai(wpk安装)外挂透明挂辅助设备(ai代打)竟然是有挂(有挂分享)-哔哩哔哩1、点击下...