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 (

相关内容

热门资讯

透视计算!红龙poker辅助工... 透视计算!红龙poker辅助工具,竞技联盟辅助插件,一直是真的有挂(扑克教程)1、游戏颠覆性的策略玩...
透视新版!we poker插件... 透视新版!we poker插件,aapoker真的假的(透视)从来是有挂(必赢方法);1、aapok...
透视安卓版!steampoke... 透视安卓版!steampokermaster辅助,pokerworld修改器,总是有挂(黑科技教程)...
透视脚本!wepoker辅助器... 透视脚本!wepoker辅助器下载,pokemmo内置修改器(透视)从来真的有挂(扑克教程);1、金...
透视ai!大菠萝789辅助器下... 透视ai!大菠萝789辅助器下载,大菠萝手游辅助,本来有挂(安装教程);1、完成大菠萝789辅助器下...
透视模拟器!佛手在线有挂吗,聚... 透视模拟器!佛手在线有挂吗,聚星ai辅助工具激活码(透视)固有是真的有挂(微扑克教程)1、聚星ai辅...
透视透视!wepoker安装教... 透视透视!wepoker安装教程,约局吧怎么看有没有挂,好像有挂(必赢教程)约局吧怎么看有没有挂辅助...
透视ai!佛手在线大菠萝辅助,... 透视ai!佛手在线大菠萝辅助,wpk辅助购买(透视)确实是真的有挂(wepoke教程)暗藏猫腻,小编...
透视智能ai!竞技联盟透视插件... 透视智能ai!竞技联盟透视插件,pokemmo辅助官网,总是真的有挂(规律教程);竞技联盟透视插件辅...
透视真的!拱趴大菠萝自动计算机... 透视真的!拱趴大菠萝自动计算机器人,werplan免费挂下载,真是真的是有挂(解密教程)1、用户打开...