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 (

相关内容

热门资讯

总算清楚!皮皮胡子辅助,圣游科... 总算清楚!皮皮胡子辅助,圣游科技辅助器,详细工具(存在有挂)1、首先打开圣游科技辅助器辅助器下载最新...
科普!广西老友玩有破解码,we... 科普!广西老友玩有破解码,wepoker内置辅助器,详细教程(有挂讲解)1、该软件可以轻松地帮助玩家...
分享一款!微信边锋辅助下载,潮... 分享一款!微信边锋辅助下载,潮友会鱼虾蟹看穿神器,详细脚本(了解有挂)1、游戏颠覆性的策略玩法,独创...
一分钟带你了解!微乐卡五星祈福... 一分钟带你了解!微乐卡五星祈福有用吗,微信闲来神器软件下载,详细神器(有挂功能)1、打开软件启动之后...
玩家必看!福建相约13水外,途... 玩家必看!福建相约13水外,途游辅助,详细软件(有挂解惑)1、进入游戏-大厅左侧-新手福利-激活码辅...
重大推荐!天天微友第三方辅助软... 重大推荐!天天微友第三方辅助软件下载,朋友局辅助器,详细挂(有挂秘籍)1、操作简单,无需天天微友第三...
记者发布!随意玩辅助器透视开挂... 记者发布!随意玩辅助器透视开挂,蜀山四川麻亲友房祈福,详细挂(证实有挂)1、随意玩辅助器透视开挂辅助...
一起来讨论!创思维激k有辅助器... 一起来讨论!创思维激k有辅助器吗,闲来辅助软件怎么样,详细app(有挂教程)1、让任何用户在无需创思...
玩家必备教程!微信微乐辅助器下... 玩家必备教程!微信微乐辅助器下载,广西友乐解码器辅助器,详细教程(真的有挂)小薇(辅助器软件下载)致...
一分钟教会你!哥哥打大a辅助,... 一分钟教会你!哥哥打大a辅助,微信微乐辅助器下载,详细方法(有挂猫腻)1、下载好微信微乐辅助器下载正...