gpt4 book ai didi

android - 文件上传 Android 4.4 WebView

转载 作者:行者123 更新时间:2023-11-29 14:13:41 28 4
gpt4 key购买 nike

我正在制作自己的 Android 应用程序,90% 的应用程序需要文件上传。

我得到了 WebView Lollipop 代码,它运行良好,非常好,但是当我尝试在 Android 4.4 中打开文件选择器时,输入不起作用。

谁能帮帮我?这是我的代码:

 mWebView.setWebChromeClient(new WebChromeClient() {

public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams) {
if(mFilePathCallback != null) {
mFilePathCallback.onReceiveValue(null);
}
mFilePathCallback = filePathCallback;

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCameraPhotoPath);
} catch (IOException ex) {
// Error occurred while creating the File
Log.e(TAG, "Unable to create Image File", ex);
}

// Continue only if the File was successfully created
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("image/*");

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, "Escoge tu imagen");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);

startActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);

return true;
}
});

最佳答案

Android 4.4 有一个破坏文件输入的 WebView 错误。我的解决方法是使用 Javascript 在应用程序和网站之间进行通信,并检测用户何时要上传文件,并从应用程序(在 webview 之外)上传文件,然后在文件加载完成时更新 webview,告诉服务器中文件所在的网站。

请在此处查看我的答案,我在此处解释了我的解决方案: https://stackoverflow.com/a/43443981/2566593

关于android - 文件上传 Android 4.4 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36903996/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com