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 (

相关内容

热门资讯

透视挂透视!wejoker黑侠... 透视挂透视!wejoker黑侠辅助器,wepoker私人局辅助(透视)新2025教程(有挂规律);1...
透视插件!wepoker有没有... 透视插件!wepoker有没有辅助,we poker辅助器下载(透视)技巧教程(有挂攻略);1、打开...
透视总结!wejoker辅助软... 透视总结!wejoker辅助软件,wepokerh5破解(透视)透明挂教程(有挂揭秘);1、在wej...
透视好友房!wepoker轻量... 透视好友房!wepoker轻量版透视,wepokerplus作弊(透视)详细教程(有挂揭秘)1、进入...
透视智能ai!wepoker开... 透视智能ai!wepoker开辅助能查到吗,wepoker有没有辅助(透视)必备教程(有挂揭秘)1、...
透视智能ai!wepoker私... 透视智能ai!wepoker私人局透视,we-poker辅助(透视)力荐教程(有挂细节)1、wepo...
透视数据!wejoker辅助脚... 透视数据!wejoker辅助脚本,we poker辅助器v3.3(透视)2025新版教程(有挂方法)...
透视游戏!wepoker可以开... 透视游戏!wepoker可以开透视吗,wepoker轻量版透视(透视)专业教程(有挂解密)1、起透看...
透视好友房!wepoker怎么... 透视好友房!wepoker怎么设置透视,we-poker辅助软件教程(透视)高科技教程(有挂方法);...
透视好友房!wepoker养号... 透视好友房!wepoker养号规律,wepoker透视有用吗(透视)扑克教程(有挂辅助);1、许多玩...