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私人局开挂视频,原来存在有挂(有挂神器)1...
热点推荐!we-poker正规... 热点推荐!we-poker正规吗,wepoker轻量版辅助,其实是真的有挂(有挂透明挂)1、模拟器是...
玩家必备教程!hhpoker怎... 玩家必备教程!hhpoker怎么开透视,wepoker脚本,都是真的有挂(揭秘有挂)1、玩家可以在透...
实测揭晓!wepoker俱乐部... 实测揭晓!wepoker俱乐部辅助器,德州透视是真的吗,竟然存在有挂(有挂技术)1、辅助软件下载优化...
推荐一款!拱趴大菠萝有什么挂,... 推荐一款!拱趴大菠萝有什么挂,hhpoker免费辅助器,一直是真的有挂(有挂攻略)1)hhpoker...
我来教教大家!wepoker作... 我来教教大家!wepoker作弊方法,淘宝买wepoker透视有用吗,原来存在有挂(今日头条)1、金...
推荐几款新版!德州局透视,如何... 推荐几款新版!德州局透视,如何判断wpk辅助软件的真假,本来真的是有挂(有挂规律)推荐几款新版!德州...
攻略讲解!hhpoker是内部... 攻略讲解!hhpoker是内部控制吗,aapoker安装包怎么使用,一贯真的是有挂(有挂秘诀)1、上...
分享一款!wepoker有人用... 分享一款!wepoker有人用过吗,wepoker怎么开辅助,切实存在有挂(确实有挂)1、打开软件启...
热点推荐!wepoker怎么挂... 热点推荐!wepoker怎么挂飞机,aapoker怎么控制牌,好像真的是有挂(有挂细节)1)辅助插件...