AndroidWebView上传文件问题
创始人
2024-10-12 03:00:47
0

Android WebView中上传文件的问题,通常出现在选择文件后无法将文件正确地上传到服务器。为了解决这个问题,需要在WebView中加入自定义的FileChooser,以便正确地处理文件上传过程。

以下是一个代码示例:

在Activity中,添加以下代码:

private static final int FILE_CHOOSER_RESULT_CODE = 1;

private ValueCallback mFilePathCallback;

private String mCameraPhotoPath;

// For Android 4.1+
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
    openFileChooser(uploadMsg, acceptType);
}

// For Android 4.0-
public void openFileChooser(ValueCallback uploadMsg) {
    openFileChooser(uploadMsg, "");
}

// For Android 3.x
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
    mFilePathCallback = new ValueCallback() {
        @Override
        public void onReceiveValue(Uri[] uris) {
            mFilePathCallback = null;
            if (uris == null || uris.length == 0) {
                return;
            }
            String filePath = GetFilePathFromUri.getPath(getApplicationContext(), uris[0]);
            if (!TextUtils.isEmpty(filePath)) {
                Uri uri = Uri.fromFile(new File(filePath));
                uploadFiles(new Uri[]{uri});
            } else {
                // Handle Error
            }
        }
    };
    Intent chooserIntent = new Intent(Intent.ACTION_GET_CONTENT);
    chooserIntent.setType(acceptType);
    startActivityForResult(chooserIntent, FILE_CHOOSER_RESULT_CODE);
}

// For Android 5.0+
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
    if (mFilePathCallback != null) {
        mFilePathCallback.onReceiveValue(null);
    }
    mFilePathCallback = filePathCallback;
    Intent intent = fileChooserParams.createIntent();
    try {
        startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);
    } catch (ActivityNotFoundException e) {
        mFilePathCallback = null;
        return false;
    }
    return true;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

相关内容

热门资讯

透视app"hh p... 透视app"hh poker辅助有用吗"详细辅助2025教程(可以免费玩透明);玩家必备必赢加哟《1...
透视私人局"poke... 透视私人局"pokemmo辅助脚本"详细辅助黑科技教程(万能挂神器)是一款可以让一直输的玩家,快速成...
透视软件"wpk模拟... 透视软件"wpk模拟器多开"详细辅助解密教程(是有人用挂透视)1、点击下载安装,微扑克wpk插件透视...
透视游戏"wepok... 透视游戏"wepoker私人局可以透视"详细辅助揭秘教程(可以透视码软件)是一款可以让一直输的玩家,...
透视真的"wepok... 透视真的"wepoker辅助透视"详细辅助黑科技教程(脚本技巧)1、很好的工具软件,可以解锁游戏的a...
透视神器"we po... 透视神器"we poker免费辅助器"详细辅助德州教程(辅助技巧),支持语音通讯、好友开房及战队组建...
透视软件"wepok... 您好,wepoker分析这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家...
透视规律"wepok... 透视规律"wepoker有透视功能吗"详细辅助AI教程(有人用过辅助);原来确实真的有挂(需添加指定...
透视攻略"aapok... 透视攻略"aapoker怎么开辅助器"详细辅助可靠技巧(免费透视技巧)科技教程也叫必备教程,这是一款...
透视免费"aapok... 透视免费"aapoker透视脚本入口"详细辅助力荐教程(辅助脚本方法)是一款可以让一直输的玩家,快速...