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 (

相关内容

热门资讯

攻略讲解,德扑之星辅助作弊挂,... 攻略讲解,德扑之星辅助作弊挂,AApoker开挂破解窍要(本来真的有辅助器)AI 是一款可以让一直输...
总算明白!,wepoker透视... 总算明白!,wepoker透视辅助挂有游戏亮点,菠萝智星德州是如何开挂的窍门(先前存在有透视)AI ...
攻略讲解,wepoker辅助器... 攻略讲解,wepoker辅助器免费下载,菠萝智星德州究竟有没有挂窍要(起初真的有辅助器)是一款可以让...
分享给玩家,wepoker作弊... 您好:wepoker作弊软件是不是真的这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会...
重大通报,德州辅助nzt有用吗... 一款 AI 游戏攻略黑科技工具上线,查攻略效率直接开挂。它整合版本更新、装备搭配、BOSS 打法、牌...
攻略讲解,wepoker线上发... 您好:wepoker线上发牌的规律这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
每日必备,wepoker线上发... 每日必备,wepoker线上发牌的规律,德扑之星到底有挂吗指南书(先前是真的有开挂)是一款可以让一直...
玩家必看科普!,微扑克辅助器有... 您好,微扑克辅助器有用吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家...
查到实测,WePoKer有没有... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
实用技巧,德扑之星能不能透视,... 实用技巧,德扑之星能不能透视,WOK有没有透视挂举措(往昔是有透视挂)是一款可以让一直输的玩家,快速...