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透视真... 您好:wepoker透视真的假的这款游戏是可以开挂的,软件加【添加微信客服136704302】确实是...
我来教教你“wepoker透视... 您好:wepoker透视辅助挂有游戏亮点的软件,软件加客服确实是有挂的,很多玩家在这款游戏中打牌都会...
玩家必备教程“wepoker辅... >>您好:wepoker辅助软件怎么用确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌特别...
最新技巧“微扑克辅助器案例”开... 您好,微扑克辅助器案例这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在...
分享认知“wpk有没有透视”开... 无需打开直接搜索;操作使用教程:分享认知“wpk有没有透视”开挂辅助挂(透视)解密教程1、界面简单,...
一起来讨论“wepoker可以... 一起来讨论“wepoker可以开挂透视吗”开挂辅助下载(透视)必赢方法是一款可以让一直输的玩家,快速...
科技通报“wpk开挂挂是真的吗... 无需打开直接搜索;操作使用教程:科技通报“wpk开挂挂是真的吗”开挂辅助脚本(透视)wpk教程1、界...
大家学习交流“wepoker私... >>您好:wepoker私人局怎么设置抽水确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户的牌...
今日科普“怎么避免wpk透视挂... 您好,怎么避免wpk透视挂这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
实测分享“wepoker透视真... >>您好:wepoker透视真的吗软件加扣扣群确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用户...