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 (

相关内容

热门资讯

玩家必看科普!hhpkoer辅... 玩家必看科普!hhpkoer辅助挂是真的吗(辅助挂)果然真的是有挂(有挂细节)1、hhpkoer辅助...
必备辅助推荐!德普之星私人局辅... 必备辅助推荐!德普之星私人局辅助免费(辅助挂)一直是真的有挂(有挂方针)一、德普之星私人局辅助免费可...
分享实测!约局吧是否有挂(辅助... 分享实测!约局吧是否有挂(辅助挂)一直真的有挂(的确有挂)约局吧是否有挂辅助器是一种具有地方特色的麻...
揭秘攻略!wepoker怎么挂... 您好,wepoker怎么挂底牌这款游戏可以开挂的,确实是有挂的,需要了解加去威信【136704302...
记者爆料!aapoker透视脚... 记者爆料!aapoker透视脚本下载(辅助挂)原来存在有挂(发现有挂)1、该软件可以轻松地帮助玩家将...
重磅来袭!hhpoker脚本下... 重磅来袭!hhpoker脚本下载(辅助挂)一贯真的有挂(有挂方略)1、每一步都需要思考,不同水平的挑...
玩家必备教程!wpk模拟器是什... 玩家必备教程!wpk模拟器是什么(辅助挂)真是有挂(有挂功能)1、该软件可以轻松地帮助玩家将wpk模...
科普常识!pokerworld... 科普常识!pokerworld修改器(辅助挂)竟然有挂(新版有挂)pokerworld修改器脚本下载...
一分钟了解!epoker有透视... 一分钟了解!epoker有透视吗(辅助挂)原来存在有挂(有人有挂)1、epoker有透视吗脚本辅助下...
热点讨论!wepoker辅助器... 热点讨论!wepoker辅助器有哪些功能(辅助挂)果然存在有挂(有挂方法)该软件可以轻松地帮助玩家将...